@middy/s3 4.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2023 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ <div align="center">
2
+ <h1>Middy s3 middleware</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4
+ <p><strong>Secrets Manager middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
5
+ <p>
6
+ <a href="https://www.npmjs.com/package/@middy/s3?activeTab=versions">
7
+ <img src="https://badge.fury.io/js/%40middy%2Fs3.svg" alt="npm version" style="max-width:100%;">
8
+ </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/s3">
10
+ <img src="https://packagephobia.com/badge?p=@middy/s3" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
13
+ <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
19
+ <a href="https://snyk.io/test/github/middyjs/middy">
20
+ <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21
+ </a>
22
+ <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
+ <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
24
+ </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
27
+ </a>
28
+ <br/>
29
+ <a href="https://gitter.im/middyjs/Lobby">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34
+ </a>
35
+ </p>
36
+ <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/s3">https://middy.js.org/docs/middlewares/s3</a></p>
37
+ </div>
38
+
39
+ ## License
40
+
41
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2023 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
42
+
43
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
44
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
45
+ </a>
package/index.cjs ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(module, "exports", {
6
+ enumerable: true,
7
+ get: ()=>_default
8
+ });
9
+ const _util = require("@middy/util");
10
+ const _clientS3 = require("@aws-sdk/client-s3");
11
+ const defaults = {
12
+ AwsClient: _clientS3.S3Client,
13
+ awsClientOptions: {},
14
+ awsClientAssumeRole: undefined,
15
+ awsClientCapture: undefined,
16
+ fetchData: {},
17
+ disablePrefetch: false,
18
+ cacheKey: 's3',
19
+ cacheExpiry: -1,
20
+ setToContext: false
21
+ };
22
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
23
+ const s3Middleware = (opts = {})=>{
24
+ const options = {
25
+ ...defaults,
26
+ ...opts
27
+ };
28
+ const fetch = (request, cachedValues = {})=>{
29
+ const values = {};
30
+ for (const internalKey of Object.keys(options.fetchData)){
31
+ if (cachedValues[internalKey]) continue;
32
+ values[internalKey] = client.send(new _clientS3.GetObjectCommand({
33
+ ...options.fetchData[internalKey],
34
+ ChecksumMode: true
35
+ })).then(async (resp)=>{
36
+ let value = await resp.Body.transformToString();
37
+ if (contentTypePattern.test(resp.headers.ContentType)) {
38
+ value = (0, _util.jsonSafeParse)(value);
39
+ }
40
+ return value;
41
+ }).catch((e)=>{
42
+ const value = (0, _util.getCache)(options.cacheKey).value ?? {};
43
+ value[internalKey] = undefined;
44
+ (0, _util.modifyCache)(options.cacheKey, value);
45
+ throw e;
46
+ });
47
+ }
48
+ return values;
49
+ };
50
+ let prefetch, client;
51
+ if ((0, _util.canPrefetch)(options)) {
52
+ client = (0, _util.createPrefetchClient)(options);
53
+ prefetch = (0, _util.processCache)(options, fetch);
54
+ }
55
+ const s3MiddlewareBefore = async (request)=>{
56
+ if (!client) {
57
+ client = await (0, _util.createClient)(options, request);
58
+ }
59
+ const { value } = prefetch ?? (0, _util.processCache)(options, fetch, request);
60
+ Object.assign(request.internal, value);
61
+ if (options.setToContext) {
62
+ const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
63
+ Object.assign(request.context, data);
64
+ }
65
+ prefetch = null;
66
+ };
67
+ return {
68
+ before: s3MiddlewareBefore
69
+ };
70
+ };
71
+ const _default = s3Middleware;
72
+
73
+
74
+ //# sourceMappingURL=index.cjs.map
package/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import middy from '@middy/core'
2
+ import { Options as MiddyOptions } from '@middy/util'
3
+ import { Context as LambdaContext } from 'aws-lambda'
4
+ import { GetObjectCommandInput, S3Client, S3ClientConfig } from '@aws-sdk/client-s3'
5
+
6
+ export type Options<AwsS3Client = S3Client> = Omit<MiddyOptions<AwsS3Client, S3ClientConfig>, 'fetchData'>
7
+ & {
8
+ fetchData?: {
9
+ [key: string]: Omit<GetObjectCommandInput, 'ChecksumMode'> & {
10
+ ChecksumMode?: never
11
+ }
12
+ }
13
+ }
14
+
15
+ export type Context<TOptions extends Options | undefined> = TOptions extends {
16
+ setToContext: true
17
+ }
18
+ ? LambdaContext & Record<keyof TOptions['fetchData'], any>
19
+ : LambdaContext
20
+
21
+ declare function s3Middleware<TOptions extends Options | undefined> (
22
+ options?: TOptions
23
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
24
+
25
+ export default s3Middleware
package/index.js ADDED
@@ -0,0 +1,66 @@
1
+ import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
+ import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
3
+ const defaults = {
4
+ AwsClient: S3Client,
5
+ awsClientOptions: {},
6
+ awsClientAssumeRole: undefined,
7
+ awsClientCapture: undefined,
8
+ fetchData: {},
9
+ disablePrefetch: false,
10
+ cacheKey: 's3',
11
+ cacheExpiry: -1,
12
+ setToContext: false
13
+ };
14
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
15
+ const s3Middleware = (opts = {})=>{
16
+ const options = {
17
+ ...defaults,
18
+ ...opts
19
+ };
20
+ const fetch = (request, cachedValues = {})=>{
21
+ const values = {};
22
+ for (const internalKey of Object.keys(options.fetchData)){
23
+ if (cachedValues[internalKey]) continue;
24
+ values[internalKey] = client.send(new GetObjectCommand({
25
+ ...options.fetchData[internalKey],
26
+ ChecksumMode: true
27
+ })).then(async (resp)=>{
28
+ let value = await resp.Body.transformToString();
29
+ if (contentTypePattern.test(resp.headers.ContentType)) {
30
+ value = jsonSafeParse(value);
31
+ }
32
+ return value;
33
+ }).catch((e)=>{
34
+ const value = getCache(options.cacheKey).value ?? {};
35
+ value[internalKey] = undefined;
36
+ modifyCache(options.cacheKey, value);
37
+ throw e;
38
+ });
39
+ }
40
+ return values;
41
+ };
42
+ let prefetch, client;
43
+ if (canPrefetch(options)) {
44
+ client = createPrefetchClient(options);
45
+ prefetch = processCache(options, fetch);
46
+ }
47
+ const s3MiddlewareBefore = async (request)=>{
48
+ if (!client) {
49
+ client = await createClient(options, request);
50
+ }
51
+ const { value } = prefetch ?? processCache(options, fetch, request);
52
+ Object.assign(request.internal, value);
53
+ if (options.setToContext) {
54
+ const data = await getInternal(Object.keys(options.fetchData), request);
55
+ Object.assign(request.context, data);
56
+ }
57
+ prefetch = null;
58
+ };
59
+ return {
60
+ before: s3MiddlewareBefore
61
+ };
62
+ };
63
+ export default s3Middleware;
64
+
65
+
66
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@middy/s3",
3
+ "version": "4.1.0",
4
+ "description": "S3 middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=16"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.cjs",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ },
21
+ "require": {
22
+ "types": "./index.d.ts",
23
+ "default": "./index.cjs"
24
+ }
25
+ }
26
+ },
27
+ "types": "index.d.ts",
28
+ "files": [
29
+ "index.js",
30
+ "index.cjs",
31
+ "index.d.ts"
32
+ ],
33
+ "scripts": {
34
+ "test": "npm run test:unit",
35
+ "test:unit": "ava",
36
+ "test:benchmark": "node __benchmarks__/index.js"
37
+ },
38
+ "license": "MIT",
39
+ "keywords": [
40
+ "Lambda",
41
+ "Middleware",
42
+ "Serverless",
43
+ "Framework",
44
+ "AWS",
45
+ "AWS Lambda",
46
+ "Middy",
47
+ "S3"
48
+ ],
49
+ "author": {
50
+ "name": "Middy contributors",
51
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "github:middyjs/middy",
56
+ "directory": "packages/s3"
57
+ },
58
+ "bugs": {
59
+ "url": "https://github.com/middyjs/middy/issues"
60
+ },
61
+ "homepage": "https://middy.js.org",
62
+ "dependencies": {
63
+ "@middy/util": "4.1.0"
64
+ },
65
+ "devDependencies": {
66
+ "@aws-sdk/client-s3": "^3.186.0",
67
+ "@middy/core": "4.1.0",
68
+ "@types/aws-lambda": "^8.10.101",
69
+ "aws-xray-sdk": "^3.3.3"
70
+ },
71
+ "gitHead": "c5abbac9280c3a2f30758188233a7b27367901d1"
72
+ }