@aws-sdk/middleware-endpoint-discovery 3.35.0 → 3.39.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/CHANGELOG.md +35 -0
- package/dist-cjs/configurations.js +0 -1
- package/dist-cjs/endpointDiscoveryMiddleware.js +0 -1
- package/dist-cjs/getCacheKey.js +0 -1
- package/dist-cjs/getEndpointDiscoveryPlugin.js +0 -1
- package/dist-cjs/index.js +2 -3
- package/dist-cjs/resolveEndpointDiscoveryConfig.js +0 -1
- package/dist-cjs/updateDiscoveredEndpointInCache.js +0 -1
- package/dist-es/configurations.js +0 -1
- package/dist-es/endpointDiscoveryMiddleware.js +0 -1
- package/dist-es/getCacheKey.js +0 -1
- package/dist-es/getEndpointDiscoveryPlugin.js +0 -1
- package/dist-es/index.js +2 -3
- package/dist-es/resolveEndpointDiscoveryConfig.js +0 -1
- package/dist-es/updateDiscoveredEndpointInCache.js +0 -1
- package/dist-types/index.d.ts +2 -2
- package/dist-types/ts3.4/getCacheKey.d.ts +1 -3
- package/dist-types/ts3.4/getEndpointDiscoveryPlugin.d.ts +2 -6
- package/dist-types/ts3.4/index.d.ts +2 -2
- package/dist-types/ts3.4/resolveEndpointDiscoveryConfig.d.ts +7 -30
- package/package.json +9 -6
- package/jest.config.js +0 -5
- package/src/configurations.spec.ts +0 -85
- package/src/configurations.ts +0 -32
- package/src/endpointDiscoveryMiddleware.spec.ts +0 -145
- package/src/endpointDiscoveryMiddleware.ts +0 -72
- package/src/getCacheKey.spec.ts +0 -32
- package/src/getCacheKey.ts +0 -24
- package/src/getEndpointDiscoveryPlugin.spec.ts +0 -32
- package/src/getEndpointDiscoveryPlugin.ts +0 -57
- package/src/index.ts +0 -3
- package/src/resolveEndpointDiscoveryConfig.spec.ts +0 -63
- package/src/resolveEndpointDiscoveryConfig.ts +0 -73
- package/src/updateDiscoveredEndpointInCache.spec.ts +0 -172
- package/src/updateDiscoveredEndpointInCache.ts +0 -88
- package/tsconfig.cjs.json +0 -9
- package/tsconfig.cjs.tsbuildinfo +0 -1
- package/tsconfig.es.json +0 -10
- package/tsconfig.es.tsbuildinfo +0 -1
- package/tsconfig.types.json +0 -9
- package/tsconfig.types.tsbuildinfo +0 -1
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { EndpointCache } from "@aws-sdk/endpoint-cache";
|
|
2
|
-
import { HttpRequest } from "@aws-sdk/protocol-http";
|
|
3
|
-
import { BuildHandlerArguments, MiddlewareStack } from "@aws-sdk/types";
|
|
4
|
-
|
|
5
|
-
import { endpointDiscoveryMiddleware } from "./endpointDiscoveryMiddleware";
|
|
6
|
-
import { getCacheKey } from "./getCacheKey";
|
|
7
|
-
import { updateDiscoveredEndpointInCache } from "./updateDiscoveredEndpointInCache";
|
|
8
|
-
|
|
9
|
-
jest.mock("./updateDiscoveredEndpointInCache");
|
|
10
|
-
jest.mock("./getCacheKey");
|
|
11
|
-
jest.mock("@aws-sdk/protocol-http");
|
|
12
|
-
|
|
13
|
-
describe(endpointDiscoveryMiddleware.name, () => {
|
|
14
|
-
const cacheKey = "cacheKey";
|
|
15
|
-
const endpoint = "endpoint";
|
|
16
|
-
const getEndpoint = jest.fn().mockReturnValue(endpoint);
|
|
17
|
-
const mockConfig = {
|
|
18
|
-
credentials: jest.fn(),
|
|
19
|
-
endpointCache: {
|
|
20
|
-
getEndpoint,
|
|
21
|
-
} as unknown as EndpointCache,
|
|
22
|
-
endpointDiscoveryEnabled: jest.fn().mockResolvedValue(undefined),
|
|
23
|
-
endpointDiscoveryEnabledProvider: jest.fn(),
|
|
24
|
-
endpointDiscoveryCommandCtor: jest.fn(),
|
|
25
|
-
isCustomEndpoint: false,
|
|
26
|
-
isClientEndpointDiscoveryEnabled: false,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const mockMiddlewareConfig = {
|
|
30
|
-
isDiscoveredEndpointRequired: false,
|
|
31
|
-
clientStack: {} as MiddlewareStack<any, any>,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const mockNext = jest.fn();
|
|
35
|
-
const mockContext = {
|
|
36
|
-
clientName: "ExampleClient",
|
|
37
|
-
commandName: "ExampleCommand",
|
|
38
|
-
};
|
|
39
|
-
const mockArgs = { request: {} };
|
|
40
|
-
|
|
41
|
-
beforeEach(() => {
|
|
42
|
-
(getCacheKey as jest.Mock).mockResolvedValue(cacheKey);
|
|
43
|
-
(updateDiscoveredEndpointInCache as jest.Mock).mockResolvedValue(undefined);
|
|
44
|
-
const { isInstance } = HttpRequest;
|
|
45
|
-
(isInstance as unknown as jest.Mock).mockReturnValue(true);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
afterEach(() => {
|
|
49
|
-
jest.clearAllMocks();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe(`isCustomEndpoint=true`, () => {
|
|
53
|
-
it(`throw error if isClientEndpointDiscoveryEnabled`, async () => {
|
|
54
|
-
try {
|
|
55
|
-
await endpointDiscoveryMiddleware(
|
|
56
|
-
{ ...mockConfig, isCustomEndpoint: true, isClientEndpointDiscoveryEnabled: true },
|
|
57
|
-
mockMiddlewareConfig
|
|
58
|
-
)(
|
|
59
|
-
mockNext,
|
|
60
|
-
mockContext
|
|
61
|
-
)(mockArgs as BuildHandlerArguments<any>);
|
|
62
|
-
fail("should throw error when isCustomEndpoint=true and isClientEndpointDiscoveryEnabled=true");
|
|
63
|
-
} catch (error) {
|
|
64
|
-
expect(error).toStrictEqual(
|
|
65
|
-
new Error(`Custom endpoint is supplied; endpointDiscoveryEnabled must not be true.`)
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
expect(mockNext).not.toHaveBeenCalled();
|
|
69
|
-
expect(updateDiscoveredEndpointInCache).not.toHaveBeenCalled();
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it(`returns without endpoint discovery`, async () => {
|
|
73
|
-
await endpointDiscoveryMiddleware({ ...mockConfig, isCustomEndpoint: true }, mockMiddlewareConfig)(
|
|
74
|
-
mockNext,
|
|
75
|
-
mockContext
|
|
76
|
-
)(mockArgs as BuildHandlerArguments<any>);
|
|
77
|
-
expect(mockNext).toHaveBeenCalledWith(mockArgs);
|
|
78
|
-
expect(updateDiscoveredEndpointInCache as jest.Mock).not.toHaveBeenCalled();
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
describe(`isDiscoveredEndpointRequired=true`, () => {
|
|
83
|
-
it(`throws error when isEndpointDiscoveryEnabled=false`, async () => {
|
|
84
|
-
mockConfig.endpointDiscoveryEnabled.mockResolvedValueOnce(false);
|
|
85
|
-
try {
|
|
86
|
-
await endpointDiscoveryMiddleware(mockConfig, { ...mockMiddlewareConfig, isDiscoveredEndpointRequired: true })(
|
|
87
|
-
mockNext,
|
|
88
|
-
mockContext
|
|
89
|
-
)(mockArgs as BuildHandlerArguments<any>);
|
|
90
|
-
fail("should throw error when isDiscoveredEndpointRequired=true and isEndpointDiscoveryEnabled=false");
|
|
91
|
-
} catch (error) {
|
|
92
|
-
expect(error).toStrictEqual(
|
|
93
|
-
new Error(
|
|
94
|
-
`Endpoint Discovery is disabled but ${mockContext.commandName} on ${mockContext.clientName} requires it.` +
|
|
95
|
-
` Please check your configurations.`
|
|
96
|
-
)
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
expect(mockNext).not.toHaveBeenCalled();
|
|
100
|
-
expect(updateDiscoveredEndpointInCache).not.toHaveBeenCalled();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe(`calls updateDiscoveredEndpointInCache`, () => {
|
|
104
|
-
it(`when isEndpointDiscoveryEnabled=undefined`, async () => {
|
|
105
|
-
await endpointDiscoveryMiddleware(mockConfig, { ...mockMiddlewareConfig, isDiscoveredEndpointRequired: true })(
|
|
106
|
-
mockNext,
|
|
107
|
-
mockContext
|
|
108
|
-
)(mockArgs as BuildHandlerArguments<any>);
|
|
109
|
-
expect(mockNext).toHaveBeenCalledWith(mockArgs);
|
|
110
|
-
expect(mockNext).toHaveBeenCalledWith({ request: { hostname: endpoint } });
|
|
111
|
-
expect(updateDiscoveredEndpointInCache).toHaveBeenCalled();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it(`when isEndpointDiscoveryEnabled=true`, async () => {
|
|
115
|
-
mockConfig.endpointDiscoveryEnabled.mockResolvedValueOnce(true);
|
|
116
|
-
await endpointDiscoveryMiddleware(mockConfig, { ...mockMiddlewareConfig, isDiscoveredEndpointRequired: true })(
|
|
117
|
-
mockNext,
|
|
118
|
-
mockContext
|
|
119
|
-
)(mockArgs as BuildHandlerArguments<any>);
|
|
120
|
-
expect(mockNext).toHaveBeenCalledWith(mockArgs);
|
|
121
|
-
expect(mockNext).toHaveBeenCalledWith({ request: { hostname: endpoint } });
|
|
122
|
-
expect(updateDiscoveredEndpointInCache).toHaveBeenCalled();
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe(`isDiscoveredEndpointRequired=false`, () => {
|
|
127
|
-
it(`calls updateDiscoveredEndpointInCache when isEndpointDiscoveryEnabled=true`, async () => {
|
|
128
|
-
mockConfig.endpointDiscoveryEnabled.mockResolvedValueOnce(true);
|
|
129
|
-
await endpointDiscoveryMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, mockContext)(
|
|
130
|
-
mockArgs as BuildHandlerArguments<any>
|
|
131
|
-
);
|
|
132
|
-
expect(mockNext).toHaveBeenCalledWith(mockArgs);
|
|
133
|
-
expect(updateDiscoveredEndpointInCache).toHaveBeenCalled();
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it(`does not call updateDiscoveredEndpointInCache when isEndpointDiscoveryEnabled=false`, async () => {
|
|
137
|
-
await endpointDiscoveryMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, mockContext)(
|
|
138
|
-
mockArgs as BuildHandlerArguments<any>
|
|
139
|
-
);
|
|
140
|
-
expect(mockNext).toHaveBeenCalledWith(mockArgs);
|
|
141
|
-
expect(updateDiscoveredEndpointInCache).not.toHaveBeenCalled();
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
});
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { HttpRequest } from "@aws-sdk/protocol-http";
|
|
2
|
-
import {
|
|
3
|
-
BuildHandler,
|
|
4
|
-
BuildHandlerArguments,
|
|
5
|
-
BuildHandlerOutput,
|
|
6
|
-
HandlerExecutionContext,
|
|
7
|
-
MetadataBearer,
|
|
8
|
-
} from "@aws-sdk/types";
|
|
9
|
-
|
|
10
|
-
import { getCacheKey } from "./getCacheKey";
|
|
11
|
-
import { EndpointDiscoveryMiddlewareConfig } from "./getEndpointDiscoveryPlugin";
|
|
12
|
-
import { EndpointDiscoveryResolvedConfig, PreviouslyResolved } from "./resolveEndpointDiscoveryConfig";
|
|
13
|
-
import { updateDiscoveredEndpointInCache } from "./updateDiscoveredEndpointInCache";
|
|
14
|
-
|
|
15
|
-
export const endpointDiscoveryMiddleware =
|
|
16
|
-
(config: EndpointDiscoveryResolvedConfig & PreviouslyResolved, middlewareConfig: EndpointDiscoveryMiddlewareConfig) =>
|
|
17
|
-
<Output extends MetadataBearer = MetadataBearer>(
|
|
18
|
-
next: BuildHandler<any, Output>,
|
|
19
|
-
context: HandlerExecutionContext
|
|
20
|
-
): BuildHandler<any, Output> =>
|
|
21
|
-
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
|
|
22
|
-
if (config.isCustomEndpoint) {
|
|
23
|
-
if (config.isClientEndpointDiscoveryEnabled) {
|
|
24
|
-
throw new Error(`Custom endpoint is supplied; endpointDiscoveryEnabled must not be true.`);
|
|
25
|
-
}
|
|
26
|
-
return next(args);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const { endpointDiscoveryCommandCtor } = config;
|
|
30
|
-
const { isDiscoveredEndpointRequired, identifiers } = middlewareConfig;
|
|
31
|
-
const { clientName, commandName } = context;
|
|
32
|
-
const isEndpointDiscoveryEnabled = await config.endpointDiscoveryEnabled();
|
|
33
|
-
const cacheKey = await getCacheKey(commandName, config, { identifiers });
|
|
34
|
-
|
|
35
|
-
if (isDiscoveredEndpointRequired) {
|
|
36
|
-
// throw error if endpoint discovery is required, and it's explicitly disabled.
|
|
37
|
-
if (isEndpointDiscoveryEnabled === false) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
`Endpoint Discovery is disabled but ${commandName} on ${clientName} requires it.` +
|
|
40
|
-
` Please check your configurations.`
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
// call await on Endpoint Discovery API utility so that function blocks
|
|
44
|
-
// till discovered endpoint is updated in cache
|
|
45
|
-
await updateDiscoveredEndpointInCache(config, {
|
|
46
|
-
...middlewareConfig,
|
|
47
|
-
commandName,
|
|
48
|
-
cacheKey,
|
|
49
|
-
endpointDiscoveryCommandCtor,
|
|
50
|
-
});
|
|
51
|
-
} else if (isEndpointDiscoveryEnabled) {
|
|
52
|
-
// Discover endpoints only if endpoint discovery is explicitly enabled.
|
|
53
|
-
// Do not call await await on Endpoint Discovery API utility so that function
|
|
54
|
-
// does not block, the command will use discovered endpoint, if available.
|
|
55
|
-
updateDiscoveredEndpointInCache(config, {
|
|
56
|
-
...middlewareConfig,
|
|
57
|
-
commandName,
|
|
58
|
-
cacheKey,
|
|
59
|
-
endpointDiscoveryCommandCtor,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const { request } = args;
|
|
64
|
-
if (cacheKey && HttpRequest.isInstance(request)) {
|
|
65
|
-
const endpoint = config.endpointCache.getEndpoint(cacheKey);
|
|
66
|
-
if (endpoint) {
|
|
67
|
-
request.hostname = endpoint;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return next(args);
|
|
72
|
-
};
|
package/src/getCacheKey.spec.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { getCacheKey } from "./getCacheKey";
|
|
2
|
-
|
|
3
|
-
describe(getCacheKey.name, () => {
|
|
4
|
-
const commandName = "commandName";
|
|
5
|
-
const mockCredentials = {
|
|
6
|
-
accessKeyId: "accessKeyId",
|
|
7
|
-
secretAccessKey: "secretAccessKey",
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const config = {
|
|
11
|
-
credentials: () => Promise.resolve(mockCredentials),
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
it("returns accessKeyId in cacheKey", async () => {
|
|
15
|
-
const cacheKey = await getCacheKey(commandName, config, {});
|
|
16
|
-
expect(cacheKey).toEqual(JSON.stringify({ accessKeyId: mockCredentials.accessKeyId }));
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("returns commandName and identifiers if passed", async () => {
|
|
20
|
-
const identifiers = { key: "value" };
|
|
21
|
-
const cacheKey = await getCacheKey(commandName, config, { identifiers });
|
|
22
|
-
expect(cacheKey).toEqual(JSON.stringify({ accessKeyId: mockCredentials.accessKeyId, commandName, identifiers }));
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("returns same cache key irrespective of key order in identifiers", async () => {
|
|
26
|
-
const identifiers1 = { key1: "value1", key2: "value2" };
|
|
27
|
-
const cacheKey1 = await getCacheKey(commandName, config, { identifiers: identifiers1 });
|
|
28
|
-
const identifiers2 = { key2: "value2", key1: "value1" };
|
|
29
|
-
const cacheKey2 = await getCacheKey(commandName, config, { identifiers: identifiers2 });
|
|
30
|
-
expect(cacheKey1).toStrictEqual(cacheKey2);
|
|
31
|
-
});
|
|
32
|
-
});
|
package/src/getCacheKey.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Credentials, Provider } from "@aws-sdk/types";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Generate key to index the endpoints in the cache
|
|
5
|
-
*/
|
|
6
|
-
export const getCacheKey = async (
|
|
7
|
-
commandName: string,
|
|
8
|
-
config: { credentials: Provider<Credentials> },
|
|
9
|
-
options: {
|
|
10
|
-
identifiers?: { [key: string]: string };
|
|
11
|
-
}
|
|
12
|
-
) => {
|
|
13
|
-
const { accessKeyId } = await config.credentials();
|
|
14
|
-
const { identifiers } = options;
|
|
15
|
-
return JSON.stringify({
|
|
16
|
-
...(accessKeyId && { accessKeyId }),
|
|
17
|
-
...(identifiers && {
|
|
18
|
-
commandName,
|
|
19
|
-
identifiers: Object.entries(identifiers)
|
|
20
|
-
.sort()
|
|
21
|
-
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
|
|
22
|
-
}),
|
|
23
|
-
});
|
|
24
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { endpointDiscoveryMiddleware } from "./endpointDiscoveryMiddleware";
|
|
2
|
-
import { endpointDiscoveryMiddlewareOptions, getEndpointDiscoveryPlugin } from "./getEndpointDiscoveryPlugin";
|
|
3
|
-
|
|
4
|
-
jest.mock("./endpointDiscoveryMiddleware");
|
|
5
|
-
|
|
6
|
-
describe(getEndpointDiscoveryPlugin.name, () => {
|
|
7
|
-
const pluginConfig = {
|
|
8
|
-
isCustomEndpoint: false,
|
|
9
|
-
endpointCache: jest.fn(),
|
|
10
|
-
endpointDiscoveryEnabled: jest.fn(),
|
|
11
|
-
isClientEndpointDiscoveryEnabled: false,
|
|
12
|
-
};
|
|
13
|
-
const middlewareConfig = {
|
|
14
|
-
isDiscoveredEndpointRequired: false,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
it(`applyToStack function adds endpoint discovery middleware`, () => {
|
|
18
|
-
const middlewareReturn = {};
|
|
19
|
-
(endpointDiscoveryMiddleware as jest.Mock).mockReturnValueOnce(middlewareReturn);
|
|
20
|
-
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
const plugin = getEndpointDiscoveryPlugin(pluginConfig, middlewareConfig);
|
|
23
|
-
const commandStack = { add: jest.fn() };
|
|
24
|
-
|
|
25
|
-
// @ts-ignore
|
|
26
|
-
plugin.applyToStack(commandStack);
|
|
27
|
-
expect(commandStack.add).toHaveBeenCalled();
|
|
28
|
-
expect(commandStack.add).toHaveBeenCalledWith(middlewareReturn, endpointDiscoveryMiddlewareOptions);
|
|
29
|
-
expect(endpointDiscoveryMiddleware).toHaveBeenCalled();
|
|
30
|
-
expect(endpointDiscoveryMiddleware).toHaveBeenCalledWith(pluginConfig, middlewareConfig);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { BuildHandlerOptions, HttpHandlerOptions, MiddlewareStack, Pluggable } from "@aws-sdk/types";
|
|
2
|
-
|
|
3
|
-
import { endpointDiscoveryMiddleware } from "./endpointDiscoveryMiddleware";
|
|
4
|
-
import { EndpointDiscoveryResolvedConfig, PreviouslyResolved } from "./resolveEndpointDiscoveryConfig";
|
|
5
|
-
|
|
6
|
-
export const endpointDiscoveryMiddlewareOptions: BuildHandlerOptions = {
|
|
7
|
-
name: "endpointDiscoveryMiddleware",
|
|
8
|
-
step: "build",
|
|
9
|
-
tags: ["ENDPOINT_DISCOVERY"],
|
|
10
|
-
override: true,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export interface EndpointDiscoveryMiddlewareConfig {
|
|
14
|
-
isDiscoveredEndpointRequired: boolean;
|
|
15
|
-
clientStack: MiddlewareStack<any, any>;
|
|
16
|
-
options?: HttpHandlerOptions;
|
|
17
|
-
identifiers?: { [key: string]: string };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const getEndpointDiscoveryPlugin = (
|
|
21
|
-
pluginConfig: EndpointDiscoveryResolvedConfig & PreviouslyResolved,
|
|
22
|
-
middlewareConfig: EndpointDiscoveryMiddlewareConfig
|
|
23
|
-
): Pluggable<any, any> => ({
|
|
24
|
-
applyToStack: (commandStack) => {
|
|
25
|
-
commandStack.add(endpointDiscoveryMiddleware(pluginConfig, middlewareConfig), endpointDiscoveryMiddlewareOptions);
|
|
26
|
-
},
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @deprecated Use getEndpointDiscoveryPlugin
|
|
31
|
-
*/
|
|
32
|
-
export const getEndpointDiscoveryRequiredPlugin = (
|
|
33
|
-
pluginConfig: EndpointDiscoveryResolvedConfig & PreviouslyResolved,
|
|
34
|
-
middlewareConfig: Omit<EndpointDiscoveryMiddlewareConfig, "isDiscoveredEndpointRequired">
|
|
35
|
-
): Pluggable<any, any> => ({
|
|
36
|
-
applyToStack: (commandStack) => {
|
|
37
|
-
commandStack.add(
|
|
38
|
-
endpointDiscoveryMiddleware(pluginConfig, { ...middlewareConfig, isDiscoveredEndpointRequired: true }),
|
|
39
|
-
endpointDiscoveryMiddlewareOptions
|
|
40
|
-
);
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @deprecated Use getEndpointDiscoveryPlugin
|
|
46
|
-
*/
|
|
47
|
-
export const getEndpointDiscoveryOptionalPlugin = (
|
|
48
|
-
pluginConfig: EndpointDiscoveryResolvedConfig & PreviouslyResolved,
|
|
49
|
-
middlewareConfig: Omit<EndpointDiscoveryMiddlewareConfig, "isDiscoveredEndpointRequired">
|
|
50
|
-
): Pluggable<any, any> => ({
|
|
51
|
-
applyToStack: (commandStack) => {
|
|
52
|
-
commandStack.add(
|
|
53
|
-
endpointDiscoveryMiddleware(pluginConfig, { ...middlewareConfig, isDiscoveredEndpointRequired: false }),
|
|
54
|
-
endpointDiscoveryMiddlewareOptions
|
|
55
|
-
);
|
|
56
|
-
},
|
|
57
|
-
});
|
package/src/index.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { EndpointCache } from "@aws-sdk/endpoint-cache";
|
|
2
|
-
|
|
3
|
-
import { resolveEndpointDiscoveryConfig } from "./resolveEndpointDiscoveryConfig";
|
|
4
|
-
|
|
5
|
-
jest.mock("@aws-sdk/endpoint-cache");
|
|
6
|
-
|
|
7
|
-
describe(resolveEndpointDiscoveryConfig.name, () => {
|
|
8
|
-
const endpointDiscoveryCommandCtor = jest.fn();
|
|
9
|
-
const mockInput = {
|
|
10
|
-
isCustomEndpoint: false,
|
|
11
|
-
credentials: jest.fn(),
|
|
12
|
-
endpointDiscoveryEnabledProvider: jest.fn(),
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
afterEach(() => {
|
|
16
|
-
jest.clearAllMocks();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("assigns endpointDiscoveryCommandCtor in resolvedConfig", () => {
|
|
20
|
-
const resolvedConfig = resolveEndpointDiscoveryConfig(mockInput, { endpointDiscoveryCommandCtor });
|
|
21
|
-
expect(resolvedConfig.endpointDiscoveryCommandCtor).toStrictEqual(endpointDiscoveryCommandCtor);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe("endpointCache", () => {
|
|
25
|
-
it("creates cache of size endpointCacheSize if passed", () => {
|
|
26
|
-
const endpointCacheSize = 100;
|
|
27
|
-
resolveEndpointDiscoveryConfig(
|
|
28
|
-
{
|
|
29
|
-
...mockInput,
|
|
30
|
-
endpointCacheSize,
|
|
31
|
-
},
|
|
32
|
-
{ endpointDiscoveryCommandCtor }
|
|
33
|
-
);
|
|
34
|
-
expect(EndpointCache).toBeCalledWith(endpointCacheSize);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("creates cache of size 1000 if endpointCacheSize not passed", () => {
|
|
38
|
-
resolveEndpointDiscoveryConfig(mockInput, { endpointDiscoveryCommandCtor });
|
|
39
|
-
expect(EndpointCache).toBeCalledWith(1000);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
describe("endpointDiscoveryEnabled", () => {
|
|
44
|
-
it.each<boolean>([false, true])(`sets to value passed in the config: %s`, (endpointDiscoveryEnabled) => {
|
|
45
|
-
const resolvedConfig = resolveEndpointDiscoveryConfig(
|
|
46
|
-
{
|
|
47
|
-
...mockInput,
|
|
48
|
-
endpointDiscoveryEnabled,
|
|
49
|
-
},
|
|
50
|
-
{ endpointDiscoveryCommandCtor }
|
|
51
|
-
);
|
|
52
|
-
expect(resolvedConfig.endpointDiscoveryEnabled()).resolves.toBe(endpointDiscoveryEnabled);
|
|
53
|
-
expect(mockInput.endpointDiscoveryEnabledProvider).not.toHaveBeenCalled();
|
|
54
|
-
expect(resolvedConfig.isClientEndpointDiscoveryEnabled).toStrictEqual(true);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it(`sets to endpointDiscoveryEnabledProvider if value is not passed`, () => {
|
|
58
|
-
const resolvedConfig = resolveEndpointDiscoveryConfig(mockInput, { endpointDiscoveryCommandCtor });
|
|
59
|
-
expect(resolvedConfig.endpointDiscoveryEnabled).toBe(mockInput.endpointDiscoveryEnabledProvider);
|
|
60
|
-
expect(resolvedConfig.isClientEndpointDiscoveryEnabled).toStrictEqual(false);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { EndpointCache } from "@aws-sdk/endpoint-cache";
|
|
2
|
-
import { Credentials, Provider } from "@aws-sdk/types";
|
|
3
|
-
|
|
4
|
-
export interface EndpointDiscoveryInputConfig {}
|
|
5
|
-
|
|
6
|
-
export interface PreviouslyResolved {
|
|
7
|
-
isCustomEndpoint: boolean;
|
|
8
|
-
credentials: Provider<Credentials>;
|
|
9
|
-
endpointDiscoveryEnabledProvider: Provider<boolean | undefined>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface EndpointDiscoveryInputConfig {
|
|
13
|
-
/**
|
|
14
|
-
* The size of the client cache storing endpoints from endpoint discovery operations.
|
|
15
|
-
* Defaults to 1000.
|
|
16
|
-
*/
|
|
17
|
-
endpointCacheSize?: number;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Whether to call operations with endpoints given by service dynamically.
|
|
21
|
-
* Setting this config to `true` will enable endpoint discovery for all applicable operations.
|
|
22
|
-
* Setting it to `false` will explicitly disable endpoint discovery even though operations that
|
|
23
|
-
* require endpoint discovery will presumably fail. Leaving it to undefined means SDK only do
|
|
24
|
-
* endpoint discovery when it's required. Defaults to `undefined`.
|
|
25
|
-
*/
|
|
26
|
-
endpointDiscoveryEnabled?: boolean | undefined;
|
|
27
|
-
}
|
|
28
|
-
export interface EndpointDiscoveryResolvedConfig {
|
|
29
|
-
/**
|
|
30
|
-
* LRU Cache which stores endpoints from endpoint discovery operations.
|
|
31
|
-
* The size is either provided by {@link EndpointDiscoveryInputConfig.endpointCacheSize}.
|
|
32
|
-
*/
|
|
33
|
-
endpointCache: EndpointCache;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The constructor of the Command used for discovering endpoints.
|
|
37
|
-
* @internal
|
|
38
|
-
*/
|
|
39
|
-
endpointDiscoveryCommandCtor: new (comandConfig: any) => any;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Resolved value for input config {@link EndpointDiscoveryInputConfig.endpointDiscoveryEnabled}.
|
|
43
|
-
*/
|
|
44
|
-
endpointDiscoveryEnabled: Provider<boolean | undefined>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Stores whether endpoint discovery configuration is set locally by passing
|
|
48
|
-
* {@link EndpointDiscoveryInputConfig.endpointDiscoveryEnabled} during client creation.
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
isClientEndpointDiscoveryEnabled: boolean;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface EndpointDiscoveryConfigOptions {
|
|
55
|
-
/**
|
|
56
|
-
* The constructor of the Command used for discovering endpoints.
|
|
57
|
-
*/
|
|
58
|
-
endpointDiscoveryCommandCtor: new (comandConfig: any) => any;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const resolveEndpointDiscoveryConfig = <T>(
|
|
62
|
-
input: T & PreviouslyResolved & EndpointDiscoveryInputConfig,
|
|
63
|
-
{ endpointDiscoveryCommandCtor }: EndpointDiscoveryConfigOptions
|
|
64
|
-
): T & EndpointDiscoveryResolvedConfig => ({
|
|
65
|
-
...input,
|
|
66
|
-
endpointDiscoveryCommandCtor,
|
|
67
|
-
endpointCache: new EndpointCache(input.endpointCacheSize ?? 1000),
|
|
68
|
-
endpointDiscoveryEnabled:
|
|
69
|
-
input.endpointDiscoveryEnabled !== undefined
|
|
70
|
-
? () => Promise.resolve(input.endpointDiscoveryEnabled)
|
|
71
|
-
: input.endpointDiscoveryEnabledProvider,
|
|
72
|
-
isClientEndpointDiscoveryEnabled: input.endpointDiscoveryEnabled !== undefined,
|
|
73
|
-
});
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { updateDiscoveredEndpointInCache } from "./updateDiscoveredEndpointInCache";
|
|
2
|
-
|
|
3
|
-
describe(updateDiscoveredEndpointInCache.name, () => {
|
|
4
|
-
const cacheKey = "cacheKey";
|
|
5
|
-
const mockGet = jest.fn();
|
|
6
|
-
const mockSet = jest.fn();
|
|
7
|
-
const mockDelete = jest.fn();
|
|
8
|
-
|
|
9
|
-
const mockHandler = jest.fn();
|
|
10
|
-
const mockResolveMiddleware = jest.fn().mockReturnValue(mockHandler);
|
|
11
|
-
|
|
12
|
-
const mockEndpoints = [{ Address: "mockAddress", CachePeriodInMinutes: 2 }];
|
|
13
|
-
const placeholderEndpoints = [{ Address: "", CachePeriodInMinutes: 1 }];
|
|
14
|
-
|
|
15
|
-
const config = {
|
|
16
|
-
endpointCache: { get: mockGet, set: mockSet, delete: mockDelete },
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const options = {
|
|
20
|
-
cacheKey,
|
|
21
|
-
commandName: "ExampleCommand",
|
|
22
|
-
endpointDiscoveryCommandCtor: jest.fn().mockReturnValue({ resolveMiddleware: mockResolveMiddleware }),
|
|
23
|
-
isDiscoveredEndpointRequired: false,
|
|
24
|
-
identifiers: { key: "value" },
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
beforeEach(() => {
|
|
28
|
-
mockGet.mockReturnValue(mockEndpoints);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
afterEach(() => {
|
|
32
|
-
jest.clearAllMocks();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it(`returns if endpoints are present in cacheKey`, async () => {
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
await updateDiscoveredEndpointInCache(config, options);
|
|
38
|
-
expect(mockGet).toHaveBeenCalledWith(cacheKey);
|
|
39
|
-
expect(mockSet).not.toHaveBeenCalled();
|
|
40
|
-
expect(mockDelete).not.toHaveBeenCalled();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
describe("calls endpointDiscoveryCommandCtor", () => {
|
|
44
|
-
beforeEach(() => {
|
|
45
|
-
mockGet.mockReturnValue(undefined);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const verifyCallsOnCacheUndefined = () => {
|
|
49
|
-
expect(mockGet).toHaveBeenCalledTimes(1);
|
|
50
|
-
|
|
51
|
-
expect(options.endpointDiscoveryCommandCtor).toHaveBeenCalledWith({
|
|
52
|
-
Operation: options.commandName.substr(0, options.commandName.length - 7),
|
|
53
|
-
Identifiers: options.identifiers,
|
|
54
|
-
});
|
|
55
|
-
expect(mockHandler).toHaveBeenCalledTimes(1);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
it("on successful call: updates cache", async () => {
|
|
59
|
-
mockHandler.mockResolvedValueOnce({ output: { Endpoints: mockEndpoints } });
|
|
60
|
-
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
await updateDiscoveredEndpointInCache(config, options);
|
|
63
|
-
|
|
64
|
-
verifyCallsOnCacheUndefined();
|
|
65
|
-
expect(mockDelete).not.toHaveBeenCalled();
|
|
66
|
-
expect(mockSet).toHaveBeenCalledTimes(2);
|
|
67
|
-
expect(mockSet).toHaveBeenNthCalledWith(1, cacheKey, placeholderEndpoints);
|
|
68
|
-
expect(mockSet).toHaveBeenNthCalledWith(2, cacheKey, mockEndpoints);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("calls endpointDiscoveryCommandCtor command just once in case of parallel calls", async () => {
|
|
72
|
-
mockHandler.mockResolvedValueOnce({ output: { Endpoints: mockEndpoints } });
|
|
73
|
-
// First call returns undefined, while other ones return placeholder endpoints a call is in progress
|
|
74
|
-
mockGet
|
|
75
|
-
.mockReturnValueOnce(undefined)
|
|
76
|
-
.mockReturnValueOnce(placeholderEndpoints)
|
|
77
|
-
.mockReturnValueOnce(placeholderEndpoints);
|
|
78
|
-
|
|
79
|
-
await Promise.all([
|
|
80
|
-
// @ts-ignore
|
|
81
|
-
updateDiscoveredEndpointInCache(config, options),
|
|
82
|
-
// @ts-ignore
|
|
83
|
-
updateDiscoveredEndpointInCache(config, options),
|
|
84
|
-
// @ts-ignore
|
|
85
|
-
updateDiscoveredEndpointInCache(config, options),
|
|
86
|
-
]);
|
|
87
|
-
|
|
88
|
-
expect(mockGet).toHaveBeenCalledTimes(3);
|
|
89
|
-
|
|
90
|
-
expect(options.endpointDiscoveryCommandCtor).toHaveBeenCalledWith({
|
|
91
|
-
Operation: options.commandName.substr(0, options.commandName.length - 7),
|
|
92
|
-
Identifiers: options.identifiers,
|
|
93
|
-
});
|
|
94
|
-
expect(mockHandler).toHaveBeenCalledTimes(1);
|
|
95
|
-
|
|
96
|
-
expect(mockDelete).not.toHaveBeenCalled();
|
|
97
|
-
expect(mockSet).toHaveBeenCalledTimes(2);
|
|
98
|
-
expect(mockSet).toHaveBeenNthCalledWith(1, cacheKey, placeholderEndpoints);
|
|
99
|
-
expect(mockSet).toHaveBeenNthCalledWith(2, cacheKey, mockEndpoints);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe("on error", () => {
|
|
103
|
-
it(`throws if isDiscoveredEndpointRequired=true`, async () => {
|
|
104
|
-
const error = new Error("rejected");
|
|
105
|
-
mockHandler.mockRejectedValueOnce(error);
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
// @ts-ignore
|
|
109
|
-
await updateDiscoveredEndpointInCache(config, { ...options, isDiscoveredEndpointRequired: true });
|
|
110
|
-
fail("updateDiscoveredEndpointInCache should throw");
|
|
111
|
-
} catch (error) {
|
|
112
|
-
expect(error).toEqual(
|
|
113
|
-
Object.assign(
|
|
114
|
-
new Error(
|
|
115
|
-
`The operation to discover endpoint failed.` +
|
|
116
|
-
` Please retry, or provide a custom endpoint and disable endpoint discovery to proceed.`
|
|
117
|
-
),
|
|
118
|
-
{ reason: error }
|
|
119
|
-
)
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
verifyCallsOnCacheUndefined();
|
|
123
|
-
expect(mockDelete).not.toHaveBeenCalled();
|
|
124
|
-
expect(mockSet).toHaveBeenCalledTimes(1);
|
|
125
|
-
expect(mockSet).toHaveBeenCalledWith(cacheKey, placeholderEndpoints);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it(`sets placeholder enpoint if isDiscoveredEndpointRequired=false`, async () => {
|
|
129
|
-
const error = new Error("rejected");
|
|
130
|
-
mockHandler.mockRejectedValueOnce(error);
|
|
131
|
-
|
|
132
|
-
// @ts-ignore
|
|
133
|
-
await updateDiscoveredEndpointInCache(config, options);
|
|
134
|
-
|
|
135
|
-
verifyCallsOnCacheUndefined();
|
|
136
|
-
expect(mockDelete).not.toHaveBeenCalled();
|
|
137
|
-
expect(mockSet).toHaveBeenCalledTimes(2);
|
|
138
|
-
expect(mockSet).toHaveBeenNthCalledWith(1, cacheKey, placeholderEndpoints);
|
|
139
|
-
expect(mockSet).toHaveBeenNthCalledWith(2, cacheKey, placeholderEndpoints);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe(`deletes cacheKey in case of`, () => {
|
|
143
|
-
const verifyCallsOnCacheKeyDelete = () => {
|
|
144
|
-
expect(mockDelete).toHaveBeenCalledWith(cacheKey);
|
|
145
|
-
expect(mockSet).toHaveBeenCalledTimes(2);
|
|
146
|
-
expect(mockSet).toHaveBeenNthCalledWith(1, cacheKey, placeholderEndpoints);
|
|
147
|
-
expect(mockSet).toHaveBeenNthCalledWith(2, cacheKey, placeholderEndpoints);
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
it(`InvalidEndpointException`, async () => {
|
|
151
|
-
const error = Object.assign(new Error("Invalid endpoint!"), { name: "InvalidEndpointException" });
|
|
152
|
-
mockHandler.mockRejectedValueOnce(error);
|
|
153
|
-
|
|
154
|
-
// @ts-ignore
|
|
155
|
-
await updateDiscoveredEndpointInCache(config, options);
|
|
156
|
-
verifyCallsOnCacheUndefined();
|
|
157
|
-
verifyCallsOnCacheKeyDelete();
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it(`Status code: 421`, async () => {
|
|
161
|
-
const error = Object.assign(new Error("Invalid endpoint!"), { $metadata: { httpStatusCode: 421 } });
|
|
162
|
-
mockHandler.mockRejectedValueOnce(error);
|
|
163
|
-
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
await updateDiscoveredEndpointInCache(config, options);
|
|
166
|
-
verifyCallsOnCacheUndefined();
|
|
167
|
-
verifyCallsOnCacheKeyDelete();
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|