@aws-amplify/core 5.5.2 → 5.5.3-api-v6.13

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 (146) hide show
  1. package/internals/aws-client-utils/composers/package.json +8 -0
  2. package/internals/aws-client-utils/package.json +4 -4
  3. package/lib/Amplify.js +2 -0
  4. package/lib/AwsClients/CognitoIdentity/base.d.ts +1 -1
  5. package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +14 -3
  6. package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +2 -1
  7. package/lib/AwsClients/CognitoIdentity/getId.d.ts +14 -3
  8. package/lib/AwsClients/CognitoIdentity/getId.js +2 -1
  9. package/lib/AwsClients/Pinpoint/getInAppMessages.d.ts +13 -3
  10. package/lib/AwsClients/Pinpoint/putEvents.d.ts +13 -3
  11. package/lib/AwsClients/Pinpoint/updateEndpoint.d.ts +13 -3
  12. package/lib/Credentials.js +2 -0
  13. package/lib/Platform/version.d.ts +1 -1
  14. package/lib/Platform/version.js +1 -1
  15. package/lib/Providers/index.js +2 -0
  16. package/lib/ServiceWorker/ServiceWorker.js +2 -0
  17. package/lib/ServiceWorker/index.js +2 -0
  18. package/lib/UniversalStorage/index.js +2 -0
  19. package/lib/Util/BackgroundProcessManager.js +2 -0
  20. package/lib/Util/DateUtils.js +2 -0
  21. package/lib/Util/Mutex.js +2 -0
  22. package/lib/Util/Reachability.js +2 -0
  23. package/lib/Util/Reachability.native.js +2 -0
  24. package/lib/Util/StringUtils.js +2 -0
  25. package/lib/Util/index.js +2 -0
  26. package/lib/clients/handlers/fetch.js +4 -21
  27. package/lib/clients/index.d.ts +7 -2
  28. package/lib/clients/index.js +17 -4
  29. package/lib/clients/internal/composeServiceApi.d.ts +1 -1
  30. package/lib/clients/internal/composeServiceApi.js +6 -6
  31. package/lib/clients/internal/index.d.ts +7 -0
  32. package/lib/clients/internal/index.js +14 -0
  33. package/lib/clients/middleware/retry/middleware.d.ts +0 -7
  34. package/lib/clients/middleware/retry/middleware.js +37 -26
  35. package/lib/clients/middleware/signing/middleware.d.ts +9 -2
  36. package/lib/clients/middleware/signing/middleware.js +25 -14
  37. package/lib/clients/middleware/signing/signer/signatureV4/index.d.ts +2 -0
  38. package/lib/clients/middleware/signing/signer/signatureV4/index.js +3 -1
  39. package/lib/clients/middleware/signing/signer/signatureV4/index.native.d.ts +1 -0
  40. package/lib/clients/middleware/signing/signer/signatureV4/index.native.js +3 -1
  41. package/lib/clients/middleware/signing/signer/signatureV4/types/signer.d.ts +10 -1
  42. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.d.ts +3 -1
  43. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.js +5 -2
  44. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.d.ts +3 -1
  45. package/lib/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.js +9 -2
  46. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignature.d.ts +1 -1
  47. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSignature.js +2 -2
  48. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.d.ts +1 -1
  49. package/lib/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.js +3 -2
  50. package/lib/clients/serde/responseInfo.js +4 -2
  51. package/lib/clients/types/aws.d.ts +4 -3
  52. package/lib/clients/utils/memoization.d.ts +9 -0
  53. package/lib/clients/utils/memoization.js +26 -0
  54. package/lib/tsconfig.tsbuildinfo +1 -1
  55. package/lib/types/index.js +2 -0
  56. package/lib-esm/Amplify.js +2 -0
  57. package/lib-esm/AwsClients/CognitoIdentity/base.d.ts +1 -1
  58. package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +14 -3
  59. package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +2 -1
  60. package/lib-esm/AwsClients/CognitoIdentity/getId.d.ts +14 -3
  61. package/lib-esm/AwsClients/CognitoIdentity/getId.js +2 -1
  62. package/lib-esm/AwsClients/Pinpoint/getInAppMessages.d.ts +13 -3
  63. package/lib-esm/AwsClients/Pinpoint/putEvents.d.ts +13 -3
  64. package/lib-esm/AwsClients/Pinpoint/updateEndpoint.d.ts +13 -3
  65. package/lib-esm/Credentials.js +2 -0
  66. package/lib-esm/Platform/version.d.ts +1 -1
  67. package/lib-esm/Platform/version.js +1 -1
  68. package/lib-esm/Providers/index.js +2 -0
  69. package/lib-esm/ServiceWorker/ServiceWorker.js +2 -0
  70. package/lib-esm/ServiceWorker/index.js +2 -0
  71. package/lib-esm/UniversalStorage/index.js +2 -0
  72. package/lib-esm/Util/BackgroundProcessManager.js +2 -0
  73. package/lib-esm/Util/DateUtils.js +2 -0
  74. package/lib-esm/Util/Mutex.js +2 -0
  75. package/lib-esm/Util/Reachability.js +2 -0
  76. package/lib-esm/Util/Reachability.native.js +2 -0
  77. package/lib-esm/Util/StringUtils.js +2 -0
  78. package/lib-esm/Util/index.js +2 -0
  79. package/lib-esm/clients/handlers/fetch.js +1 -18
  80. package/lib-esm/clients/index.d.ts +7 -2
  81. package/lib-esm/clients/index.js +7 -2
  82. package/lib-esm/clients/internal/composeServiceApi.d.ts +1 -1
  83. package/lib-esm/clients/internal/composeServiceApi.js +6 -6
  84. package/lib-esm/clients/internal/index.d.ts +7 -0
  85. package/lib-esm/clients/internal/index.js +9 -0
  86. package/lib-esm/clients/middleware/retry/middleware.d.ts +0 -7
  87. package/lib-esm/clients/middleware/retry/middleware.js +36 -24
  88. package/lib-esm/clients/middleware/signing/middleware.d.ts +9 -2
  89. package/lib-esm/clients/middleware/signing/middleware.js +25 -14
  90. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.d.ts +2 -0
  91. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.js +1 -0
  92. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.native.d.ts +1 -0
  93. package/lib-esm/clients/middleware/signing/signer/signatureV4/index.native.js +1 -0
  94. package/lib-esm/clients/middleware/signing/signer/signatureV4/types/signer.d.ts +10 -1
  95. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.d.ts +3 -1
  96. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.js +5 -2
  97. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.d.ts +3 -1
  98. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.js +9 -2
  99. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignature.d.ts +1 -1
  100. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSignature.js +2 -2
  101. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.d.ts +1 -1
  102. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.js +3 -2
  103. package/lib-esm/clients/serde/responseInfo.js +3 -1
  104. package/lib-esm/clients/types/aws.d.ts +4 -3
  105. package/lib-esm/clients/utils/memoization.d.ts +9 -0
  106. package/lib-esm/clients/utils/memoization.js +22 -0
  107. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  108. package/lib-esm/types/index.js +2 -0
  109. package/package.json +8 -8
  110. package/polyfills/URL/index.ts +2 -0
  111. package/src/Amplify.ts +2 -0
  112. package/src/AwsClients/CognitoIdentity/getCredentialsForIdentity.ts +1 -1
  113. package/src/AwsClients/CognitoIdentity/getId.ts +1 -1
  114. package/src/Credentials.ts +2 -0
  115. package/src/Platform/version.ts +1 -1
  116. package/src/Providers/index.ts +2 -0
  117. package/src/ServiceWorker/ServiceWorker.ts +2 -0
  118. package/src/ServiceWorker/index.ts +2 -0
  119. package/src/UniversalStorage/index.ts +2 -0
  120. package/src/Util/BackgroundProcessManager.ts +2 -0
  121. package/src/Util/DateUtils.ts +2 -0
  122. package/src/Util/Mutex.ts +2 -0
  123. package/src/Util/Reachability.native.ts +2 -0
  124. package/src/Util/Reachability.ts +2 -0
  125. package/src/Util/Retry.ts +2 -0
  126. package/src/Util/StringUtils.ts +2 -0
  127. package/src/Util/index.ts +2 -0
  128. package/src/clients/handlers/fetch.ts +1 -19
  129. package/src/clients/index.ts +15 -2
  130. package/src/clients/internal/composeServiceApi.ts +12 -6
  131. package/src/clients/internal/index.ts +10 -0
  132. package/src/clients/middleware/retry/middleware.ts +28 -24
  133. package/src/clients/middleware/signing/middleware.ts +13 -2
  134. package/src/clients/middleware/signing/signer/signatureV4/index.native.ts +1 -0
  135. package/src/clients/middleware/signing/signer/signatureV4/index.ts +2 -0
  136. package/src/clients/middleware/signing/signer/signatureV4/types/signer.ts +14 -1
  137. package/src/clients/middleware/signing/signer/signatureV4/utils/getCanonicalRequest.ts +7 -7
  138. package/src/clients/middleware/signing/signer/signatureV4/utils/getCanonicalUri.ts +11 -2
  139. package/src/clients/middleware/signing/signer/signatureV4/utils/getSignature.ts +2 -1
  140. package/src/clients/middleware/signing/signer/signatureV4/utils/getSigningValues.ts +2 -0
  141. package/src/clients/serde/responseInfo.ts +4 -2
  142. package/src/clients/types/aws.ts +4 -3
  143. package/src/clients/utils/memoization.ts +23 -0
  144. package/src/parseAWSExports.ts +2 -0
  145. package/src/types/index.ts +2 -0
  146. package/src/types/types.ts +2 -0
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  /**
2
4
  * TODO: [v6] remove this polyfill.
3
5
  *
package/src/Amplify.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { ConsoleLogger as LoggerClass } from './Logger';
2
4
 
3
5
  const logger = new LoggerClass('Amplify');
@@ -11,11 +11,11 @@ import {
11
11
  Endpoint,
12
12
  HttpRequest,
13
13
  HttpResponse,
14
- composeServiceApi,
15
14
  parseJsonBody,
16
15
  parseJsonError,
17
16
  parseMetadata,
18
17
  } from '../../clients';
18
+ import { composeServiceApi } from '../../clients/internal';
19
19
  import type {
20
20
  GetCredentialsForIdentityCommandInput as GetCredentialsForIdentityInput,
21
21
  GetCredentialsForIdentityCommandOutput as GetCredentialsForIdentityOutput,
@@ -11,11 +11,11 @@ import {
11
11
  Endpoint,
12
12
  HttpRequest,
13
13
  HttpResponse,
14
- composeServiceApi,
15
14
  parseJsonBody,
16
15
  parseJsonError,
17
16
  parseMetadata,
18
17
  } from '../../clients';
18
+ import { composeServiceApi } from '../../clients/internal';
19
19
  import {
20
20
  GetIdCommandInput as GetIdInput,
21
21
  GetIdCommandOutput as GetIdOutput,
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { ConsoleLogger as Logger } from './Logger';
2
4
  import { StorageHelper } from './StorageHelper';
3
5
  import { makeQuerablePromise } from './JS';
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '5.3.3';
2
+ export const version = '5.3.4-api-v6.13+5927d05d2';
@@ -1 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  export { AWSCloudWatchProvider } from './AWSCloudWatchProvider';
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  /**
2
4
  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
5
  *
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  /**
2
4
  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
5
  *
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import Cookies, { CookieSetOptions } from 'universal-cookie';
2
4
  import { browserOrNode } from '../JS';
3
5
 
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  /**
2
4
  * @private For internal Amplify use.
3
5
  *
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  /**
2
4
  * Date & time utility functions to abstract the `aws-sdk` away from users.
3
5
  * (v2 => v3 modularization is a breaking change)
package/src/Util/Mutex.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  /*!
2
4
  * The MIT License (MIT)
3
5
  *
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import Observable from 'zen-observable-ts';
2
4
  import { ConsoleLogger as Logger } from '../Logger';
3
5
 
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { browserOrNode, isWebWorker } from '@aws-amplify/core';
2
4
  import Observable, { ZenObservable } from 'zen-observable-ts';
3
5
 
package/src/Util/Retry.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { DelayFunction } from '../types';
2
4
  import { ConsoleLogger as Logger } from '../Logger/ConsoleLogger';
3
5
  const logger = new Logger('Util');
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  export function urlSafeEncode(str: string) {
2
4
  return str
3
5
  .split('')
package/src/Util/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  export {
2
4
  NonRetryableError,
3
5
  isNonRetryableError,
@@ -4,6 +4,7 @@
4
4
  import 'isomorphic-unfetch'; // TODO: remove this dependency in v6
5
5
  import { HttpRequest, HttpResponse, HttpTransferOptions } from '../types/http';
6
6
  import { TransferHandler } from '../types/core';
7
+ import { withMemoization } from '../utils/memoization';
7
8
 
8
9
  const shouldSendBody = (method: string) =>
9
10
  !['HEAD', 'GET', 'DELETE'].includes(method.toUpperCase());
@@ -54,22 +55,3 @@ export const fetchTransferHandler: TransferHandler<
54
55
  body: bodyWithMixin,
55
56
  };
56
57
  };
57
-
58
- /**
59
- * Cache the payload of a response body. It allows multiple calls to the body,
60
- * for example, when reading the body in both retry decider and error deserializer.
61
- * Caching body is allowed here because we call the body accessor(blob(), json(),
62
- * etc.) when body is small or streaming implementation is not available(RN).
63
- */
64
- const withMemoization = <T>(payloadAccessor: () => Promise<T>) => {
65
- let cached: Promise<T>;
66
- return () => {
67
- if (!cached) {
68
- // Explicitly not awaiting. Intermediate await would add overhead and
69
- // introduce a possible race in the event that this wrapper is called
70
- // again before the first `payloadAccessor` call resolves.
71
- cached = payloadAccessor();
72
- }
73
- return cached;
74
- };
75
- };
@@ -6,10 +6,23 @@ export { getDnsSuffix } from './endpoints';
6
6
  export { fetchTransferHandler } from './handlers/fetch';
7
7
  export { unauthenticatedHandler } from './handlers/unauthenticated';
8
8
  export { authenticatedHandler } from './handlers/authenticated';
9
- export { composeServiceApi } from './internal/composeServiceApi';
10
9
  export {
11
- signRequest,
10
+ getHashedPayload,
12
11
  presignUrl,
12
+ PresignUrlOptions,
13
+ signRequest,
14
+ SignRequestOptions,
13
15
  } from './middleware/signing/signer/signatureV4';
16
+ export { EMPTY_HASH as EMPTY_SHA256_HASH } from './middleware/signing/signer/signatureV4/constants';
17
+ export { extendedEncodeURIComponent } from './middleware/signing/utils/extendedEncodeURIComponent';
18
+ export { signingMiddleware, SigningOptions } from './middleware/signing';
19
+ export {
20
+ getRetryDecider,
21
+ jitteredBackoff,
22
+ retryMiddleware,
23
+ RetryOptions,
24
+ } from './middleware/retry';
25
+ export { userAgentMiddleware, UserAgentOptions } from './middleware/userAgent';
14
26
  export { parseJsonBody, parseJsonError, parseMetadata } from './serde';
27
+ export { withMemoization } from './utils/memoization';
15
28
  export * from './types';
@@ -16,13 +16,16 @@ export const composeServiceApi = <
16
16
  HttpResponse,
17
17
  TransferHandlerOptions
18
18
  >,
19
- serializer: (input: Input, endpoint: Endpoint) => HttpRequest,
19
+ serializer: (
20
+ input: Input,
21
+ endpoint: Endpoint
22
+ ) => Promise<HttpRequest> | HttpRequest,
20
23
  deserializer: (output: HttpResponse) => Promise<Output>,
21
24
  defaultConfig: DefaultConfig
22
25
  ) => {
23
26
  return async (
24
27
  config: OptionalizeKey<
25
- TransferHandlerOptions & ServiceClientOptions,
28
+ TransferHandlerOptions & ServiceClientOptions & DefaultConfig,
26
29
  keyof DefaultConfig
27
30
  >,
28
31
  input: Input
@@ -31,13 +34,16 @@ export const composeServiceApi = <
31
34
  ...defaultConfig,
32
35
  ...config,
33
36
  } as unknown as TransferHandlerOptions & ServiceClientOptions;
34
- const endpoint = await resolvedConfig.endpointResolver({
35
- region: resolvedConfig.region,
36
- });
37
+ // We may want to allow different endpoints from given config(other than region) and input.
38
+ // Currently S3 supports additional `useAccelerateEndpoint` option to use accelerate endpoint.
39
+ const endpoint = await resolvedConfig.endpointResolver(
40
+ resolvedConfig,
41
+ input
42
+ );
37
43
  // Unlike AWS SDK clients, a serializer should NOT populate the `host` or `content-length` headers.
38
44
  // Both of these headers are prohibited per Spec(https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name).
39
45
  // They will be populated automatically by browser, or node-fetch polyfill.
40
- const request = serializer(input, endpoint);
46
+ const request = await serializer(input, endpoint);
41
47
  const response = await transferHandler(request, {
42
48
  ...resolvedConfig,
43
49
  });
@@ -0,0 +1,10 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * This index should NOT be included in the ../index.ts because the composer functions
6
+ * here requires TypeScript >= 4. Given other scoped packages may use TypeScript < 4,
7
+ * they should not be exposed to upper level index.
8
+ */
9
+ export { composeServiceApi } from './composeServiceApi';
10
+ export { composeTransferHandler } from './composeTransferHandler';
@@ -1,7 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { MetadataBearer } from '@aws-sdk/types';
5
4
  import {
6
5
  MiddlewareContext,
7
6
  MiddlewareHandler,
@@ -60,13 +59,25 @@ export const retryMiddleware = <TInput = Request, TOutput = Response>({
60
59
  let error: Error;
61
60
  let attemptsCount = context.attemptsCount ?? 0;
62
61
  let response: TOutput;
62
+
63
+ // When retry is not needed or max attempts is reached, either error or response will be set. This function handles either cases.
64
+ const handleTerminalErrorOrResponse = () => {
65
+ if (response) {
66
+ addOrIncrementMetadataAttempts(response, attemptsCount);
67
+ return response;
68
+ } else {
69
+ addOrIncrementMetadataAttempts(error, attemptsCount);
70
+ throw error;
71
+ }
72
+ };
73
+
63
74
  while (!abortSignal?.aborted && attemptsCount < maxAttempts) {
64
- error = undefined;
65
- response = undefined;
66
75
  try {
67
76
  response = await next(request);
77
+ error = undefined;
68
78
  } catch (e) {
69
79
  error = e;
80
+ response = undefined;
70
81
  }
71
82
  // context.attemptsCount may be updated after calling next handler which may retry the request by itself.
72
83
  attemptsCount =
@@ -81,17 +92,16 @@ export const retryMiddleware = <TInput = Request, TOutput = Response>({
81
92
  await cancellableSleep(delay, abortSignal);
82
93
  }
83
94
  continue;
84
- } else if (response) {
85
- updateMetadataAttempts(response, attemptsCount);
86
- return response;
87
95
  } else {
88
- updateMetadataAttempts(error, attemptsCount);
89
- throw error;
96
+ return handleTerminalErrorOrResponse();
90
97
  }
91
98
  }
92
- throw abortSignal?.aborted
93
- ? new Error('Request aborted')
94
- : error ?? new Error('Retry attempts exhausted');
99
+
100
+ if (abortSignal?.aborted) {
101
+ throw new Error('Request aborted.');
102
+ } else {
103
+ return handleTerminalErrorOrResponse();
104
+ }
95
105
  };
96
106
  };
97
107
 
@@ -113,21 +123,15 @@ const cancellableSleep = (timeoutMs: number, abortSignal?: AbortSignal) => {
113
123
  return sleepPromise;
114
124
  };
115
125
 
116
- /**
117
- * Check if the response is a contains `$metadata` property.
118
- *
119
- * @internal Amplify internal use only
120
- */
121
- export const isMetadataBearer = (
122
- response: unknown
123
- ): response is MetadataBearer => typeof response?.['$metadata'] === 'object';
124
-
125
- const updateMetadataAttempts = (
126
+ const addOrIncrementMetadataAttempts = (
126
127
  nextHandlerOutput: Object,
127
128
  attempts: number
128
129
  ) => {
129
- if (isMetadataBearer(nextHandlerOutput)) {
130
- nextHandlerOutput.$metadata.attempts = attempts;
130
+ if (Object.prototype.toString.call(nextHandlerOutput) !== '[object Object]') {
131
+ return;
131
132
  }
132
- nextHandlerOutput['$metadata'] = { attempts };
133
+ nextHandlerOutput['$metadata'] = {
134
+ ...(nextHandlerOutput['$metadata'] ?? {}),
135
+ attempts,
136
+ };
133
137
  };
@@ -15,9 +15,17 @@ import { getUpdatedSystemClockOffset } from './utils/getUpdatedSystemClockOffset
15
15
  * Configuration of the signing middleware
16
16
  */
17
17
  export interface SigningOptions {
18
- credentials: Credentials;
18
+ credentials: Credentials | (() => Promise<Credentials>);
19
19
  region: string;
20
20
  service: string;
21
+
22
+ /**
23
+ * Whether to uri encode the path as part of canonical uri. It's used for S3 only where the pathname
24
+ * is already uri encoded, and the signing process is not expected to uri encode it again.
25
+ *
26
+ * @default true
27
+ */
28
+ uriEscapePath?: boolean;
21
29
  }
22
30
 
23
31
  /**
@@ -28,16 +36,19 @@ export const signingMiddleware = ({
28
36
  credentials,
29
37
  region,
30
38
  service,
39
+ uriEscapePath = true,
31
40
  }: SigningOptions) => {
32
41
  let currentSystemClockOffset;
33
42
  return (next: MiddlewareHandler<HttpRequest, HttpResponse>) =>
34
43
  async function signingMiddleware(request: HttpRequest) {
35
44
  currentSystemClockOffset = currentSystemClockOffset ?? 0;
36
45
  const signRequestOptions = {
37
- credentials,
46
+ credentials:
47
+ typeof credentials === 'function' ? await credentials() : credentials,
38
48
  signingDate: getSkewCorrectedDate(currentSystemClockOffset),
39
49
  signingRegion: region,
40
50
  signingService: service,
51
+ uriEscapePath,
41
52
  };
42
53
  const signedRequest = await signRequest(request, signRequestOptions);
43
54
  const response = await next(signedRequest);
@@ -7,3 +7,4 @@ import '@aws-amplify/core/polyfills/URL';
7
7
  export { signRequest } from './signRequest';
8
8
  export { presignUrl } from './presignUrl';
9
9
  export { TOKEN_QUERY_PARAM } from './constants';
10
+ export { getHashedPayload } from './utils/getHashedPayload';
@@ -5,3 +5,5 @@
5
5
  export { signRequest } from './signRequest';
6
6
  export { presignUrl } from './presignUrl';
7
7
  export { TOKEN_QUERY_PARAM } from './constants';
8
+ export { getHashedPayload } from './utils/getHashedPayload';
9
+ export type { SignRequestOptions, PresignUrlOptions } from './types';
@@ -8,6 +8,16 @@ export interface SignRequestOptions {
8
8
  signingDate?: Date;
9
9
  signingRegion: string;
10
10
  signingService: string;
11
+
12
+ /**
13
+ * Whether to uri encode the path as part of canonical uri. It's used for S3 only where the pathname
14
+ * is already uri encoded, and the signing process is not expected to uri encode it again.
15
+ *
16
+ * @default true
17
+ *
18
+ * @see https://github.com/aws/aws-sdk-js-v3/blob/9ba012dfa3a3429aa2db0f90b3b0b3a7a31f9bc3/packages/signature-v4/src/SignatureV4.ts#L76-L83
19
+ */
20
+ uriEscapePath?: boolean;
11
21
  }
12
22
 
13
23
  export interface PresignUrlOptions extends SignRequestOptions {
@@ -26,6 +36,9 @@ export interface FormattedDates {
26
36
  export interface SigningValues
27
37
  extends Credentials,
28
38
  FormattedDates,
29
- Pick<SignRequestOptions, 'signingRegion' | 'signingService'> {
39
+ Pick<
40
+ SignRequestOptions,
41
+ 'signingRegion' | 'signingService' | 'uriEscapePath'
42
+ > {
30
43
  credentialScope: string;
31
44
  }
@@ -12,6 +12,8 @@ import { getSignedHeaders } from './getSignedHeaders';
12
12
  * Returns a canonical request.
13
13
  *
14
14
  * @param request `HttpRequest` from which to create the canonical request from.
15
+ * @param uriEscapePath Whether to uri encode the path as part of canonical uri. It's used for S3 only where the
16
+ * pathname is already uri encoded, and the signing process is not expected to uri encode it again. Defaults to true.
15
17
  * @returns String created by by concatenating the following strings, separated by newline characters:
16
18
  * - HTTPMethod
17
19
  * - CanonicalUri
@@ -22,15 +24,13 @@ import { getSignedHeaders } from './getSignedHeaders';
22
24
  *
23
25
  * @internal
24
26
  */
25
- export const getCanonicalRequest = ({
26
- body,
27
- headers,
28
- method,
29
- url,
30
- }: HttpRequest): string =>
27
+ export const getCanonicalRequest = (
28
+ { body, headers, method, url }: HttpRequest,
29
+ uriEscapePath = true
30
+ ): string =>
31
31
  [
32
32
  method,
33
- getCanonicalUri(url.pathname),
33
+ getCanonicalUri(url.pathname, uriEscapePath),
34
34
  getCanonicalQueryString(url.searchParams),
35
35
  getCanonicalHeaders(headers),
36
36
  getSignedHeaders(headers),
@@ -5,10 +5,19 @@
5
5
  * Returns a canonical uri.
6
6
  *
7
7
  * @param pathname `pathname` from request url.
8
+ * @param uriEscapePath Whether to uri encode the path as part of canonical uri. It's used for S3 only where the
9
+ * pathname is already uri encoded, and the signing process is not expected to uri encode it again. Defaults to true.
8
10
  * @returns URI-encoded version of the absolute path component URL (everything between the host and the question mark
9
11
  * character (?) that starts the query string parameters). If the absolute path is empty, a forward slash character (/).
10
12
  *
11
13
  * @internal
12
14
  */
13
- export const getCanonicalUri = (pathname: string): string =>
14
- pathname ? encodeURIComponent(pathname).replace(/%2F/g, '/') : '/';
15
+ export const getCanonicalUri = (
16
+ pathname: string,
17
+ uriEscapePath = true
18
+ ): string =>
19
+ pathname
20
+ ? uriEscapePath
21
+ ? encodeURIComponent(pathname).replace(/%2F/g, '/')
22
+ : pathname
23
+ : '/';
@@ -27,10 +27,11 @@ export const getSignature = (
27
27
  shortDate,
28
28
  signingRegion,
29
29
  signingService,
30
+ uriEscapePath,
30
31
  }: SigningValues
31
32
  ): string => {
32
33
  // step 1: create a canonical request
33
- const canonicalRequest = getCanonicalRequest(request);
34
+ const canonicalRequest = getCanonicalRequest(request, uriEscapePath);
34
35
 
35
36
  // step 2: create a hash of the canonical request
36
37
  const hashedRequest = getHashedDataAsHex(null, canonicalRequest);
@@ -19,6 +19,7 @@ export const getSigningValues = ({
19
19
  signingDate = new Date(),
20
20
  signingRegion,
21
21
  signingService,
22
+ uriEscapePath = true,
22
23
  }: SignRequestOptions): SigningValues => {
23
24
  // get properties from credentials
24
25
  const { accessKeyId, secretAccessKey, sessionToken } = credentials;
@@ -39,5 +40,6 @@ export const getSigningValues = ({
39
40
  shortDate,
40
41
  signingRegion,
41
42
  signingService,
43
+ uriEscapePath,
42
44
  };
43
45
  };
@@ -1,8 +1,7 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { ResponseMetadata } from '@aws-sdk/types';
5
- import { isMetadataBearer } from '../middleware/retry/middleware';
4
+ import { ResponseMetadata, MetadataBearer } from '@aws-sdk/types';
6
5
  import { HttpResponse } from '../types/http';
7
6
 
8
7
  export const parseMetadata = (response: HttpResponse): ResponseMetadata => {
@@ -18,3 +17,6 @@ export const parseMetadata = (response: HttpResponse): ResponseMetadata => {
18
17
  cfId: headers['x-amz-cf-id'],
19
18
  };
20
19
  };
20
+
21
+ const isMetadataBearer = (response: unknown): response is MetadataBearer =>
22
+ typeof response?.['$metadata'] === 'object';
@@ -1,10 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { MetadataBearer } from '@aws-sdk/types';
4
5
  import { Endpoint } from './core';
5
6
  import { HttpResponse } from './http';
6
7
 
7
- export type { Credentials } from '@aws-sdk/types';
8
+ export type { Credentials, MetadataBearer } from '@aws-sdk/types';
8
9
 
9
10
  export type SourceData = string | ArrayBuffer | ArrayBufferView;
10
11
 
@@ -15,7 +16,7 @@ export type EndpointResolverOptions = { region: string };
15
16
 
16
17
  export interface ServiceClientOptions {
17
18
  region: string;
18
- endpointResolver: (options: EndpointResolverOptions) => Endpoint;
19
+ endpointResolver: (options: EndpointResolverOptions, input?: any) => Endpoint;
19
20
  }
20
21
 
21
22
  /**
@@ -24,4 +25,4 @@ export interface ServiceClientOptions {
24
25
  */
25
26
  export type ErrorParser = (
26
27
  response?: HttpResponse
27
- ) => Promise<Error | undefined>;
28
+ ) => Promise<(Error & MetadataBearer) | undefined>;
@@ -0,0 +1,23 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Cache the payload of a response body. It allows multiple calls to the body,
6
+ * for example, when reading the body in both retry decider and error deserializer.
7
+ * Caching body is allowed here because we call the body accessor(blob(), json(),
8
+ * etc.) when body is small or streaming implementation is not available(RN).
9
+ *
10
+ * @internal
11
+ */
12
+ export const withMemoization = <T>(payloadAccessor: () => Promise<T>) => {
13
+ let cached: Promise<T>;
14
+ return () => {
15
+ if (!cached) {
16
+ // Explicitly not awaiting. Intermediate await would add overhead and
17
+ // introduce a possible race in the event that this wrapper is called
18
+ // again before the first `payloadAccessor` call resolves.
19
+ cached = payloadAccessor();
20
+ }
21
+ return cached;
22
+ };
23
+ };
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { AmplifyConfig } from './types';
2
4
  import { ConsoleLogger as Logger } from './Logger';
3
5
 
@@ -1 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  export * from './types';
@@ -1,3 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
1
3
  import { InputLogEvent, LogGroup } from '@aws-sdk/client-cloudwatch-logs';
2
4
  import { Credentials } from '@aws-sdk/types';
3
5