@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.
- package/index.js +76 -51
- package/package.json +6 -5
package/index.js
CHANGED
|
@@ -1,54 +1,79 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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
|
+
"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
|
|
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
|
|
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": "
|
|
71
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
71
72
|
}
|