@middy/service-discovery 6.1.6 → 6.2.1
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.d.ts +48 -42
- package/index.js +65 -65
- package/package.json +71 -74
package/index.d.ts
CHANGED
|
@@ -1,50 +1,56 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from
|
|
1
|
+
import type {
|
|
2
|
+
DiscoverInstancesCommandInput,
|
|
3
|
+
HttpInstanceSummary,
|
|
4
|
+
ServiceDiscoveryClient,
|
|
5
|
+
ServiceDiscoveryClientConfig,
|
|
6
|
+
} from "@aws-sdk/client-servicediscovery";
|
|
7
|
+
import type middy from "@middy/core";
|
|
8
|
+
import type { Options as MiddyOptions } from "@middy/util";
|
|
9
|
+
import type { Context as LambdaContext } from "aws-lambda";
|
|
10
10
|
|
|
11
|
-
interface ServiceDiscoveryOptions<
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
> {
|
|
25
|
-
fetchData?: { [key: string]: DiscoverInstancesCommandInput }
|
|
11
|
+
interface ServiceDiscoveryOptions<
|
|
12
|
+
AwsServiceDiscoveryClient = ServiceDiscoveryClient,
|
|
13
|
+
> extends Pick<
|
|
14
|
+
MiddyOptions<AwsServiceDiscoveryClient, ServiceDiscoveryClientConfig>,
|
|
15
|
+
| "AwsClient"
|
|
16
|
+
| "awsClientOptions"
|
|
17
|
+
| "awsClientCapture"
|
|
18
|
+
| "disablePrefetch"
|
|
19
|
+
| "cacheKey"
|
|
20
|
+
| "cacheExpiry"
|
|
21
|
+
| "setToContext"
|
|
22
|
+
> {
|
|
23
|
+
fetchData?: { [key: string]: DiscoverInstancesCommandInput };
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
export type Context<TOptions extends ServiceDiscoveryOptions | undefined> =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
TOptions extends { setToContext: true }
|
|
28
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
29
|
+
? LambdaContext & {
|
|
30
|
+
[Key in keyof TFetchData]: HttpInstanceSummary[];
|
|
31
|
+
}
|
|
32
|
+
: never
|
|
33
|
+
: LambdaContext;
|
|
36
34
|
|
|
37
35
|
export type Internal<TOptions extends ServiceDiscoveryOptions | undefined> =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
TOptions extends ServiceDiscoveryOptions
|
|
37
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
38
|
+
? {
|
|
39
|
+
[Key in keyof TFetchData]: HttpInstanceSummary[];
|
|
40
|
+
}
|
|
41
|
+
: {}
|
|
42
|
+
: {};
|
|
45
43
|
|
|
46
|
-
declare function serviceDiscovery<
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
declare function serviceDiscovery<
|
|
45
|
+
TOptions extends ServiceDiscoveryOptions | undefined,
|
|
46
|
+
>(
|
|
47
|
+
options?: TOptions,
|
|
48
|
+
): middy.MiddlewareObj<
|
|
49
|
+
unknown,
|
|
50
|
+
any,
|
|
51
|
+
Error,
|
|
52
|
+
Context<TOptions>,
|
|
53
|
+
Internal<TOptions>
|
|
54
|
+
>;
|
|
49
55
|
|
|
50
|
-
export default serviceDiscovery
|
|
56
|
+
export default serviceDiscovery;
|
package/index.js
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
getCache,
|
|
6
|
-
getInternal,
|
|
7
|
-
processCache,
|
|
8
|
-
modifyCache,
|
|
9
|
-
catchInvalidSignatureException
|
|
10
|
-
} from '@middy/util'
|
|
2
|
+
DiscoverInstancesCommand,
|
|
3
|
+
ServiceDiscoveryClient,
|
|
4
|
+
} from "@aws-sdk/client-servicediscovery";
|
|
11
5
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
canPrefetch,
|
|
7
|
+
catchInvalidSignatureException,
|
|
8
|
+
createClient,
|
|
9
|
+
createPrefetchClient,
|
|
10
|
+
getCache,
|
|
11
|
+
getInternal,
|
|
12
|
+
modifyCache,
|
|
13
|
+
processCache,
|
|
14
|
+
} from "@middy/util";
|
|
15
15
|
|
|
16
16
|
const defaults = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
17
|
+
AwsClient: ServiceDiscoveryClient,
|
|
18
|
+
awsClientOptions: {},
|
|
19
|
+
awsClientAssumeRole: undefined,
|
|
20
|
+
awsClientCapture: undefined,
|
|
21
|
+
fetchData: {}, // { contextKey: {NamespaceName, ServiceName, HealthStatus} }
|
|
22
|
+
disablePrefetch: false,
|
|
23
|
+
cacheKey: "cloud-map",
|
|
24
|
+
cacheKeyExpiry: {},
|
|
25
|
+
cacheExpiry: -1,
|
|
26
|
+
setToContext: false,
|
|
27
|
+
};
|
|
28
28
|
|
|
29
29
|
const serviceDiscoveryMiddleware = (opts = {}) => {
|
|
30
|
-
|
|
30
|
+
const options = { ...defaults, ...opts };
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const fetchRequest = (request, cachedValues = {}) => {
|
|
33
|
+
const values = {};
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
for (const internalKey of Object.keys(options.fetchData)) {
|
|
36
|
+
if (cachedValues[internalKey]) continue;
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
const command = new DiscoverInstancesCommand(
|
|
39
|
+
options.fetchData[internalKey],
|
|
40
|
+
);
|
|
41
|
+
values[internalKey] = client
|
|
42
|
+
.send(command)
|
|
43
|
+
.catch((e) => catchInvalidSignatureException(e, client, command))
|
|
44
|
+
.then((resp) => resp.Instances)
|
|
45
|
+
.catch((e) => {
|
|
46
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
47
|
+
value[internalKey] = undefined;
|
|
48
|
+
modifyCache(options.cacheKey, value);
|
|
49
|
+
throw e;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
return values;
|
|
54
|
+
};
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
let client;
|
|
57
|
+
if (canPrefetch(options)) {
|
|
58
|
+
client = createPrefetchClient(options);
|
|
59
|
+
processCache(options, fetchRequest);
|
|
60
|
+
}
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
const serviceDiscoveryMiddlewareBefore = async (request) => {
|
|
63
|
+
if (!client) {
|
|
64
|
+
client = await createClient(options, request);
|
|
65
|
+
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
const { value } = processCache(options, fetchRequest, request);
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
Object.assign(request.internal, value);
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
if (options.setToContext) {
|
|
72
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
73
|
+
if (options.setToContext) Object.assign(request.context, data);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
export default serviceDiscoveryMiddleware
|
|
77
|
+
return {
|
|
78
|
+
before: serviceDiscoveryMiddlewareBefore,
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
export default serviceDiscoveryMiddleware;
|
package/package.json
CHANGED
|
@@ -1,76 +1,73 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"aws-xray-sdk": "^3.3.3"
|
|
74
|
-
},
|
|
75
|
-
"gitHead": "b84840ec8afd289f6decfd0d645be4899051792d"
|
|
2
|
+
"name": "@middy/service-discovery",
|
|
3
|
+
"version": "6.2.1",
|
|
4
|
+
"description": "Service Discovery (Cloud Map) instances middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"Service Discovery",
|
|
43
|
+
"SCloud Map",
|
|
44
|
+
"Instances"
|
|
45
|
+
],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Middy contributors",
|
|
48
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
53
|
+
"directory": "packages/sts"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://middy.js.org",
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "github",
|
|
61
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@middy/util": "6.2.1"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@aws-sdk/client-servicediscovery": "^3.0.0",
|
|
68
|
+
"@middy/core": "6.2.1",
|
|
69
|
+
"@types/aws-lambda": "^8.10.101",
|
|
70
|
+
"aws-xray-sdk": "^3.3.3"
|
|
71
|
+
},
|
|
72
|
+
"gitHead": "b84840ec8afd289f6decfd0d645be4899051792d"
|
|
76
73
|
}
|