@middy/service-discovery 5.0.3 → 5.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.
Files changed (2) hide show
  1. package/index.js +76 -51
  2. package/package.json +6 -5
package/index.js CHANGED
@@ -1,54 +1,79 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util';
2
- import { ServiceDiscoveryClient, DiscoverInstancesCommand } from '@aws-sdk/client-servicediscovery';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache
9
+ } from '@middy/util'
10
+ import {
11
+ ServiceDiscoveryClient,
12
+ DiscoverInstancesCommand
13
+ } from '@aws-sdk/client-servicediscovery'
14
+
3
15
  const defaults = {
4
- AwsClient: ServiceDiscoveryClient,
5
- awsClientOptions: {},
6
- awsClientAssumeRole: undefined,
7
- awsClientCapture: undefined,
8
- fetchData: {},
9
- disablePrefetch: false,
10
- cacheKey: 'cloud-map',
11
- cacheKeyExpiry: {},
12
- cacheExpiry: -1,
13
- setToContext: false
14
- };
15
- const serviceDiscoveryMiddleware = (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 DiscoverInstancesCommand(options.fetchData[internalKey])).then((resp)=>resp.Instances).catch((e)=>{
25
- const value = getCache(options.cacheKey).value ?? {};
26
- value[internalKey] = undefined;
27
- modifyCache(options.cacheKey, value);
28
- throw e;
29
- });
30
- }
31
- return values;
32
- };
33
- let client;
34
- if (canPrefetch(options)) {
35
- client = createPrefetchClient(options);
36
- processCache(options, fetch);
16
+ AwsClient: ServiceDiscoveryClient,
17
+ awsClientOptions: {},
18
+ awsClientAssumeRole: undefined,
19
+ awsClientCapture: undefined,
20
+ fetchData: {}, // { contextKey: {NamespaceName, ServiceName, HealthStatus} }
21
+ disablePrefetch: false,
22
+ cacheKey: 'cloud-map',
23
+ cacheKeyExpiry: {},
24
+ cacheExpiry: -1,
25
+ setToContext: false
26
+ }
27
+
28
+ const serviceDiscoveryMiddleware = (opts = {}) => {
29
+ const options = { ...defaults, ...opts }
30
+
31
+ const fetch = (request, cachedValues = {}) => {
32
+ const values = {}
33
+
34
+ for (const internalKey of Object.keys(options.fetchData)) {
35
+ if (cachedValues[internalKey]) continue
36
+
37
+ const command = new DiscoverInstancesCommand(
38
+ options.fetchData[internalKey]
39
+ )
40
+ values[internalKey] = client
41
+ .send(command)
42
+ .then((resp) => resp.Instances)
43
+ .catch((e) => {
44
+ const value = getCache(options.cacheKey).value ?? {}
45
+ value[internalKey] = undefined
46
+ modifyCache(options.cacheKey, value)
47
+ throw e
48
+ })
49
+ }
50
+
51
+ return values
52
+ }
53
+
54
+ let client
55
+ if (canPrefetch(options)) {
56
+ client = createPrefetchClient(options)
57
+ processCache(options, fetch)
58
+ }
59
+
60
+ const serviceDiscoveryMiddlewareBefore = async (request) => {
61
+ if (!client) {
62
+ client = await createClient(options, request)
63
+ }
64
+
65
+ const { value } = processCache(options, fetch, request)
66
+
67
+ Object.assign(request.internal, value)
68
+
69
+ if (options.setToContext) {
70
+ const data = await getInternal(Object.keys(options.fetchData), request)
71
+ if (options.setToContext) Object.assign(request.context, data)
37
72
  }
38
- const serviceDiscoveryMiddlewareBefore = async (request)=>{
39
- if (!client) {
40
- client = await createClient(options, request);
41
- }
42
- const { value } = processCache(options, fetch, request);
43
- Object.assign(request.internal, value);
44
- if (options.setToContext) {
45
- const data = await getInternal(Object.keys(options.fetchData), request);
46
- if (options.setToContext) Object.assign(request.context, data);
47
- }
48
- };
49
- return {
50
- before: serviceDiscoveryMiddlewareBefore
51
- };
52
- };
53
- export default serviceDiscoveryMiddleware;
73
+ }
54
74
 
75
+ return {
76
+ before: serviceDiscoveryMiddlewareBefore
77
+ }
78
+ }
79
+ export default serviceDiscoveryMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/service-discovery",
3
- "version": "5.0.3",
3
+ "version": "5.2.0",
4
4
  "description": "Service Discovery (Cloud Map) instances middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -26,7 +26,8 @@
26
26
  ],
27
27
  "scripts": {
28
28
  "test": "npm run test:unit",
29
- "test:unit": "ava"
29
+ "test:unit": "ava",
30
+ "test:benchmark": "node __benchmarks__/index.js"
30
31
  },
31
32
  "license": "MIT",
32
33
  "keywords": [
@@ -59,13 +60,13 @@
59
60
  "url": "https://github.com/sponsors/willfarrell"
60
61
  },
61
62
  "dependencies": {
62
- "@middy/util": "5.0.3"
63
+ "@middy/util": "5.2.0"
63
64
  },
64
65
  "devDependencies": {
65
66
  "@aws-sdk/client-servicediscovery": "^3.0.0",
66
- "@middy/core": "5.0.3",
67
+ "@middy/core": "5.2.0",
67
68
  "@types/aws-lambda": "^8.10.101",
68
69
  "aws-xray-sdk": "^3.3.3"
69
70
  },
70
- "gitHead": "87660575a7ac2b52e4153c407a4c63c9449dcd0d"
71
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
71
72
  }