@middy/rds-signer 2.5.2 → 2.5.6

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 (3) hide show
  1. package/index.d.ts +2 -13
  2. package/index.js +40 -56
  3. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -1,19 +1,8 @@
1
1
  import { RDS } from 'aws-sdk'
2
- import { captureAWSClient } from 'aws-xray-sdk'
3
2
  import middy from '@middy/core'
3
+ import { Options as MiddyOptions } from '@middy/util'
4
4
 
5
- interface Options<Signer = RDS.Signer> {
6
- AwsClient?: new() => Signer
7
- awsClientOptions?: Partial<RDS.Types.ClientConfiguration>
8
- awsClientAssumeRole?: string
9
- awsClientCapture?: typeof captureAWSClient
10
- fetchData?: { [key: string]: string }
11
- disablePrefetch?: boolean
12
- cacheKey?: string
13
- cacheExpiry?: number
14
- setToEnv?: boolean
15
- setToContext?: boolean
16
- }
5
+ interface Options<Signer = RDS.Signer> extends MiddyOptions<Signer, RDS.Types.ClientConfiguration> {}
17
6
 
18
7
  declare function rdsSigner (options?: Options): middy.MiddlewareObj
19
8
 
package/index.js CHANGED
@@ -1,98 +1,82 @@
1
- "use strict";
2
-
3
1
  const {
4
2
  canPrefetch,
5
3
  getInternal,
6
4
  processCache,
7
5
  getCache,
8
6
  modifyCache
9
- } = require('@middy/util');
10
-
11
- const {
12
- Signer
13
- } = require('aws-sdk/clients/rds.js'); // v2
7
+ } = require('@middy/util')
8
+ const { Signer } = require('aws-sdk/clients/rds.js') // v2
14
9
  // const { RDS:{Signer} } = require('@aws-sdk/client-rds') // v3
15
10
 
16
-
17
11
  const defaults = {
18
12
  AwsClient: Signer,
19
13
  awsClientOptions: {},
20
- fetchData: {},
21
- // { contextKey: {region, hostname, username, database, port} }
14
+ fetchData: {}, // { contextKey: {region, hostname, username, database, port} }
22
15
  disablePrefetch: false,
23
16
  cacheKey: 'rds-signer',
24
17
  cacheExpiry: -1,
25
18
  setToEnv: false,
26
19
  setToContext: false
27
- };
20
+ }
28
21
 
29
22
  const rdsSignerMiddleware = (opts = {}) => {
30
- const options = { ...defaults,
31
- ...opts
32
- };
23
+ const options = { ...defaults, ...opts }
33
24
 
34
25
  const fetch = (request, cachedValues = {}) => {
35
- const values = {};
36
-
26
+ const values = {}
37
27
  for (const internalKey of Object.keys(options.fetchData)) {
38
- if (cachedValues[internalKey]) continue;
39
- const client = new options.AwsClient({ ...options.awsClientOptions,
40
- ...options.fetchData[internalKey]
41
- }); // AWS doesn't support getAuthToken.promise() in aws-sdk v2 :( See https://github.com/aws/aws-sdk-js/issues/3595
28
+ if (cachedValues[internalKey]) continue
42
29
 
30
+ const client = new options.AwsClient({
31
+ ...options.awsClientOptions,
32
+ ...options.fetchData[internalKey]
33
+ })
34
+ // AWS doesn't support getAuthToken.promise() in aws-sdk v2 :( See https://github.com/aws/aws-sdk-js/issues/3595
43
35
  values[internalKey] = new Promise((resolve, reject) => {
44
36
  client.getAuthToken({}, (err, token) => {
45
37
  if (err) {
46
- reject(err);
47
- } // Catch Missing token, this usually means their is something wrong with the credentials
48
-
49
-
38
+ reject(err)
39
+ }
40
+ // Catch Missing token, this usually means their is something wrong with the credentials
50
41
  if (!token.includes('X-Amz-Security-Token=')) {
51
- reject(new Error('X-Amz-Security-Token Missing'));
42
+ reject(new Error('X-Amz-Security-Token Missing'))
52
43
  }
53
-
54
- resolve(token);
55
- });
56
- }).catch(e => {
57
- var _getCache$value, _getCache;
58
-
59
- const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
60
- value[internalKey] = undefined;
61
- modifyCache(options.cacheKey, value);
62
- throw e;
63
- }); // aws-sdk v3
44
+ resolve(token)
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
+ // aws-sdk v3
64
53
  // values[internalKey] = createClient(awsClientOptions, request).then(client => client.getAuthToken())
65
54
  }
66
55
 
67
- return values;
68
- };
69
-
70
- let prefetch;
56
+ return values
57
+ }
71
58
 
59
+ let prefetch
72
60
  if (canPrefetch(options)) {
73
- prefetch = processCache(options, fetch);
61
+ prefetch = processCache(options, fetch)
74
62
  }
75
63
 
76
- const rdsSignerMiddlewareBefore = async request => {
77
- var _prefetch;
64
+ const rdsSignerMiddlewareBefore = async (request) => {
65
+ const { value } = prefetch ?? processCache(options, fetch, request)
78
66
 
79
- const {
80
- value
81
- } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
82
- Object.assign(request.internal, value);
67
+ Object.assign(request.internal, value)
83
68
 
84
69
  if (options.setToContext || options.setToEnv) {
85
- const data = await getInternal(Object.keys(options.fetchData), request);
86
- if (options.setToEnv) Object.assign(process.env, data);
87
- if (options.setToContext) Object.assign(request.context, data);
70
+ const data = await getInternal(Object.keys(options.fetchData), request)
71
+ if (options.setToEnv) Object.assign(process.env, data)
72
+ if (options.setToContext) Object.assign(request.context, data)
88
73
  }
89
74
 
90
- prefetch = null;
91
- };
75
+ prefetch = null
76
+ }
92
77
 
93
78
  return {
94
79
  before: rdsSignerMiddlewareBefore
95
- };
96
- };
97
-
98
- module.exports = rdsSignerMiddleware;
80
+ }
81
+ }
82
+ module.exports = rdsSignerMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/rds-signer",
3
- "version": "2.5.2",
3
+ "version": "2.5.6",
4
4
  "description": "RDS (Relational Database Service) credentials middleware for the middy framework",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -46,13 +46,13 @@
46
46
  },
47
47
  "homepage": "https://github.com/middyjs/middy#readme",
48
48
  "dependencies": {
49
- "@middy/util": "^2.5.2"
49
+ "@middy/util": "^2.5.6"
50
50
  },
51
51
  "devDependencies": {
52
- "@middy/core": "^2.5.2",
52
+ "@middy/core": "^2.5.6",
53
53
  "@types/node": "^16.0.0",
54
54
  "aws-sdk": "^2.939.0",
55
55
  "aws-xray-sdk": "^3.3.3"
56
56
  },
57
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
57
+ "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
58
58
  }