Skip to content

Commit 2fb1c79

Browse files
initial aws sdk v3 compatiblity
1 parent 3704bb4 commit 2fb1c79

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

bin/cdklocal

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const importLib = function importLib (libPath) {
139139
}
140140
};
141141

142+
// this isn't doing anything for current versions (e.g. 2.167.1)
142143
const setSdkOptions = async (options, setHttpOptions) => {
143144
if (!useLocal(options)) {
144145
return;
@@ -155,10 +156,13 @@ const setSdkOptions = async (options, setHttpOptions) => {
155156
const patchProviderCredentials = (provider) => {
156157
const origConstr = provider.SdkProvider.withAwsCliCompatibleDefaults;
157158
provider.SdkProvider.withAwsCliCompatibleDefaults = async (options = {}) => {
158-
await setSdkOptions(options, true);
159+
const localEndpoint = await getLocalEndpoint();
160+
await setSdkOptions(options, true); // legacy
159161
const result = await origConstr(options);
160-
result.sdkOptions = result.sdkOptions || {};
161-
await setSdkOptions(result.sdkOptions);
162+
result.sdkOptions = result.sdkOptions || {}; // legacy
163+
await setSdkOptions(result.sdkOptions); // legacy
164+
result.requestHandler.endpoint = localEndpoint;
165+
result.requestHandler.forcePathStyle = true;
162166
return result;
163167
};
164168

@@ -351,7 +355,51 @@ const isEsbuildBundle = () => {
351355
}
352356
};
353357

358+
359+
const patchSdk = (SDK, localEndpoint) => {
360+
getMethods(SDK.prototype).forEach((methodName) => {
361+
if (typeof SDK.prototype[methodName] === 'function') {
362+
const original = SDK.prototype[methodName];
363+
SDK.prototype[methodName] = function methFunc (...args) {
364+
this.config = {
365+
...this.config,
366+
endpoint: localEndpoint,
367+
forcePathStyle: true,
368+
};
369+
return original.apply(this, args);
370+
};
371+
}
372+
});
373+
};
374+
375+
let sdkOverwritten = false;
376+
const patchSdkProvider = (provider, SDK) => {
377+
getMethods(provider.SdkProvider.prototype).forEach((methodName) => {
378+
if (typeof provider.SdkProvider.prototype[methodName] === 'function') {
379+
const original = provider.SdkProvider.prototype[methodName];
380+
provider.SdkProvider.prototype[methodName] = async function methFunc(...args) {
381+
const localEndpoint = await getLocalEndpoint();
382+
383+
if (!sdkOverwritten) {
384+
// the goal is to support `SdkProvider.withAssumedRole`
385+
// since it instantiates a different client (i.e. not from the SDK class)
386+
this.requestHandler.endpoint = localEndpoint;
387+
this.requestHandler.forcePathStyle = true;
388+
// patch SDK class methods (mostly clients) to make sure the config that is created in the constructor
389+
// is updated with the correct configuration
390+
patchSdk(SDK, localEndpoint);
391+
sdkOverwritten = true;
392+
}
393+
return await original.apply(this, args);
394+
};
395+
}
396+
}
397+
);
398+
};
399+
354400
const applyPatches = (provider, CdkToolkit, SDK, ToolkitInfo, patchAssets = true) => {
401+
patchSdkProvider(provider, SDK);
402+
// TODO: a lot of the patches are not really needed for newer versions
355403
patchProviderCredentials(provider);
356404
patchCdkToolkit(CdkToolkit);
357405
patchCurrentAccount(SDK);

0 commit comments

Comments
 (0)