@middy/sts 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 +71 -0
  2. package/package.json +7 -5
package/index.js ADDED
@@ -0,0 +1,71 @@
1
+ import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util';
2
+ import STS from 'aws-sdk/clients/sts.js';
3
+ const defaults = {
4
+ AwsClient: STS,
5
+ awsClientOptions: {},
6
+ awsClientCapture: undefined,
7
+ fetchData: {},
8
+ disablePrefetch: false,
9
+ cacheKey: 'sts',
10
+ cacheExpiry: -1,
11
+ setToContext: false
12
+ };
13
+
14
+ const stsMiddleware = (opts = {}) => {
15
+ const options = { ...defaults,
16
+ ...opts
17
+ };
18
+
19
+ const fetch = (request, cachedValues = {}) => {
20
+ const values = {};
21
+
22
+ for (const internalKey of Object.keys(options.fetchData)) {
23
+ if (cachedValues[internalKey]) continue;
24
+ const assumeRoleOptions = options.fetchData[internalKey];
25
+ assumeRoleOptions.RoleSessionName ?? (assumeRoleOptions.RoleSessionName = 'middy-sts-session-' + Math.ceil(Math.random() * 99999));
26
+ values[internalKey] = client.assumeRole(assumeRoleOptions).promise().then(resp => ({
27
+ accessKeyId: resp.Credentials.AccessKeyId,
28
+ secretAccessKey: resp.Credentials.SecretAccessKey,
29
+ sessionToken: resp.Credentials.SessionToken
30
+ })).catch(e => {
31
+ const value = getCache(options.cacheKey).value ?? {};
32
+ value[internalKey] = undefined;
33
+ modifyCache(options.cacheKey, value);
34
+ throw e;
35
+ });
36
+ }
37
+
38
+ return values;
39
+ };
40
+
41
+ let prefetch, client;
42
+
43
+ if (canPrefetch(options)) {
44
+ client = createPrefetchClient(options);
45
+ prefetch = processCache(options, fetch);
46
+ }
47
+
48
+ const stsMiddlewareBefore = async request => {
49
+ if (!client) {
50
+ client = await createClient(options, request);
51
+ }
52
+
53
+ const {
54
+ value
55
+ } = prefetch ?? processCache(options, fetch, request);
56
+ Object.assign(request.internal, value);
57
+
58
+ if (options.setToContext) {
59
+ const data = await getInternal(Object.keys(options.fetchData), request);
60
+ if (options.setToContext) Object.assign(request.context, data);
61
+ }
62
+
63
+ prefetch = null;
64
+ };
65
+
66
+ return {
67
+ before: stsMiddlewareBefore
68
+ };
69
+ };
70
+
71
+ export default stsMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/sts",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "STS (Security Token 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,12 +48,12 @@
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
  "aws-sdk": "^2.939.0",
54
56
  "aws-xray-sdk": "^3.3.3"
55
57
  },
56
- "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
58
+ "gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
57
59
  }