@middy/rds-signer 3.0.0-alpha.0 → 3.0.0-alpha.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.
Files changed (2) hide show
  1. package/index.js +74 -0
  2. package/package.json +7 -5
package/index.js ADDED
@@ -0,0 +1,74 @@
1
+ import { canPrefetch, getInternal, processCache, getCache, modifyCache } from '@middy/util';
2
+ import RDS from 'aws-sdk/clients/rds.js';
3
+ const defaults = {
4
+ AwsClient: RDS.Signer,
5
+ awsClientOptions: {},
6
+ fetchData: {},
7
+ disablePrefetch: false,
8
+ cacheKey: 'rds-signer',
9
+ cacheExpiry: -1,
10
+ setToContext: false
11
+ };
12
+
13
+ const rdsSignerMiddleware = (opts = {}) => {
14
+ const options = { ...defaults,
15
+ ...opts
16
+ };
17
+
18
+ const fetch = (request, cachedValues = {}) => {
19
+ const values = {};
20
+
21
+ for (const internalKey of Object.keys(options.fetchData)) {
22
+ if (cachedValues[internalKey]) continue;
23
+ const client = new options.AwsClient({ ...options.awsClientOptions,
24
+ ...options.fetchData[internalKey]
25
+ });
26
+ values[internalKey] = new Promise((resolve, reject) => {
27
+ client.getAuthToken({}, (e, token) => {
28
+ if (e) {
29
+ reject(e);
30
+ }
31
+
32
+ if (!token.includes('X-Amz-Security-Token=')) {
33
+ reject(new Error('X-Amz-Security-Token Missing'));
34
+ }
35
+
36
+ resolve(token);
37
+ });
38
+ }).catch(e => {
39
+ const value = getCache(options.cacheKey).value ?? {};
40
+ value[internalKey] = undefined;
41
+ modifyCache(options.cacheKey, value);
42
+ throw e;
43
+ });
44
+ }
45
+
46
+ return values;
47
+ };
48
+
49
+ let prefetch;
50
+
51
+ if (canPrefetch(options)) {
52
+ prefetch = processCache(options, fetch);
53
+ }
54
+
55
+ const rdsSignerMiddlewareBefore = async request => {
56
+ const {
57
+ value
58
+ } = prefetch ?? processCache(options, fetch, request);
59
+ Object.assign(request.internal, value);
60
+
61
+ if (options.setToContext) {
62
+ const data = await getInternal(Object.keys(options.fetchData), request);
63
+ Object.assign(request.context, data);
64
+ }
65
+
66
+ prefetch = null;
67
+ };
68
+
69
+ return {
70
+ before: rdsSignerMiddlewareBefore
71
+ };
72
+ };
73
+
74
+ export default rdsSignerMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/rds-signer",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "RDS (Relational Database Service) credentials middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -13,11 +13,13 @@
13
13
  "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -46,13 +48,13 @@
46
48
  },
47
49
  "homepage": "https://github.com/middyjs/middy#readme",
48
50
  "dependencies": {
49
- "@middy/util": "^3.0.0-alpha.0"
51
+ "@middy/util": "^3.0.0-alpha.4"
50
52
  },
51
53
  "devDependencies": {
52
- "@middy/core": "^3.0.0-alpha.0",
54
+ "@middy/core": "^3.0.0-alpha.4",
53
55
  "@types/node": "^17.0.0",
54
56
  "aws-sdk": "^2.939.0",
55
57
  "aws-xray-sdk": "^3.3.3"
56
58
  },
57
- "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
59
+ "gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
58
60
  }