@aws-amplify/storage 5.6.4-api-v6.2 → 5.6.4-api-v6.23
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/lib/AwsClients/S3/abortMultipartUpload.d.ts +21 -0
- package/lib/AwsClients/S3/abortMultipartUpload.js +85 -0
- package/lib/AwsClients/S3/base.d.ts +54 -0
- package/lib/AwsClients/S3/base.js +75 -0
- package/lib/AwsClients/S3/completeMultipartUpload.d.ts +20 -0
- package/lib/AwsClients/S3/completeMultipartUpload.js +162 -0
- package/lib/AwsClients/S3/copyObject.d.ts +24 -0
- package/lib/AwsClients/S3/copyObject.js +98 -0
- package/lib/AwsClients/S3/createMultipartUpload.d.ts +21 -0
- package/lib/AwsClients/S3/createMultipartUpload.js +95 -0
- package/lib/AwsClients/S3/deleteObject.d.ts +20 -0
- package/lib/AwsClients/S3/deleteObject.js +86 -0
- package/lib/AwsClients/S3/getObject.d.ts +27 -0
- package/lib/AwsClients/S3/getObject.js +216 -0
- package/lib/AwsClients/S3/headObject.d.ts +20 -0
- package/lib/AwsClients/S3/headObject.js +96 -0
- package/lib/AwsClients/S3/index.d.ts +13 -0
- package/lib/AwsClients/S3/index.js +30 -0
- package/lib/AwsClients/S3/index.native.d.ts +14 -0
- package/lib/AwsClients/S3/index.native.js +31 -0
- package/lib/AwsClients/S3/listObjectsV2.d.ts +20 -0
- package/lib/AwsClients/S3/listObjectsV2.js +146 -0
- package/lib/AwsClients/S3/listParts.d.ts +20 -0
- package/lib/AwsClients/S3/listParts.js +110 -0
- package/lib/AwsClients/S3/putObject.d.ts +23 -0
- package/lib/AwsClients/S3/putObject.js +95 -0
- package/lib/AwsClients/S3/runtime/base64/index.browser.d.ts +2 -0
- package/lib/AwsClients/S3/runtime/base64/index.browser.js +20 -0
- package/lib/AwsClients/S3/runtime/base64/index.native.d.ts +2 -0
- package/lib/AwsClients/S3/runtime/base64/index.native.js +17 -0
- package/lib/AwsClients/S3/runtime/constants.d.ts +9 -0
- package/lib/AwsClients/S3/runtime/constants.js +14 -0
- package/lib/AwsClients/S3/runtime/contentSha256middleware.d.ts +9 -0
- package/lib/AwsClients/S3/runtime/contentSha256middleware.js +70 -0
- package/lib/AwsClients/S3/runtime/index.browser.d.ts +5 -0
- package/lib/AwsClients/S3/runtime/index.browser.js +20 -0
- package/lib/AwsClients/S3/runtime/index.d.ts +5 -0
- package/lib/AwsClients/S3/runtime/index.js +21 -0
- package/lib/AwsClients/S3/runtime/index.native.d.ts +5 -0
- package/lib/AwsClients/S3/runtime/index.native.js +20 -0
- package/lib/AwsClients/S3/runtime/s3TransferHandler/fetch.d.ts +8 -0
- package/lib/AwsClients/S3/runtime/s3TransferHandler/fetch.js +15 -0
- package/lib/AwsClients/S3/runtime/s3TransferHandler/xhr.d.ts +8 -0
- package/lib/AwsClients/S3/runtime/s3TransferHandler/xhr.js +21 -0
- package/lib/AwsClients/S3/runtime/xhrTransferHandler.d.ts +22 -0
- package/lib/AwsClients/S3/runtime/xhrTransferHandler.js +201 -0
- package/lib/AwsClients/S3/runtime/xmlParser/dom.d.ts +8 -0
- package/lib/AwsClients/S3/runtime/xmlParser/dom.js +92 -0
- package/lib/AwsClients/S3/runtime/xmlParser/pureJs.d.ts +10 -0
- package/lib/AwsClients/S3/runtime/xmlParser/pureJs.js +57 -0
- package/lib/AwsClients/S3/types.d.ts +2857 -0
- package/lib/AwsClients/S3/types.js +4 -0
- package/lib/AwsClients/S3/uploadPart.d.ts +20 -0
- package/lib/AwsClients/S3/uploadPart.js +98 -0
- package/lib/AwsClients/S3/utils/deserializeHelpers.d.ts +78 -0
- package/lib/AwsClients/S3/utils/deserializeHelpers.js +153 -0
- package/lib/AwsClients/S3/utils/index.d.ts +5 -0
- package/lib/AwsClients/S3/utils/index.js +30 -0
- package/lib/AwsClients/S3/utils/parsePayload.d.ts +3 -0
- package/lib/AwsClients/S3/utils/parsePayload.js +96 -0
- package/lib/AwsClients/S3/utils/serializeHelpers.d.ts +42 -0
- package/lib/AwsClients/S3/utils/serializeHelpers.js +183 -0
- package/lib/Storage.d.ts +5 -5
- package/lib/Storage.js +61 -31
- package/lib/common/MD5utils.d.ts +1 -0
- package/lib/common/MD5utils.js +79 -0
- package/lib/common/MD5utils.native.d.ts +1 -0
- package/lib/common/MD5utils.native.js +83 -0
- package/lib/common/S3ClientUtils.d.ts +24 -15
- package/lib/common/S3ClientUtils.js +83 -95
- package/lib/common/StorageConstants.d.ts +0 -1
- package/lib/common/StorageConstants.js +3 -2
- package/lib/common/StorageErrorStrings.js +3 -1
- package/lib/common/StorageUtils.js +12 -6
- package/lib/index.js +19 -4
- package/lib/providers/AWSS3Provider.d.ts +4 -8
- package/lib/providers/AWSS3Provider.js +210 -139
- package/lib/providers/AWSS3ProviderManagedUpload.d.ts +7 -7
- package/lib/providers/AWSS3ProviderManagedUpload.js +205 -99
- package/lib/providers/AWSS3UploadTask.d.ts +8 -8
- package/lib/providers/AWSS3UploadTask.js +226 -129
- package/lib/providers/index.js +4 -2
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/AWSS3Provider.d.ts +62 -62
- package/lib/types/AWSS3Provider.js +0 -1
- package/lib/types/Provider.d.ts +1 -1
- package/lib/types/Provider.js +0 -1
- package/lib/types/Storage.d.ts +23 -23
- package/lib/types/Storage.js +0 -1
- package/lib/types/index.js +17 -1
- package/lib-esm/AwsClients/S3/abortMultipartUpload.d.ts +21 -0
- package/lib-esm/AwsClients/S3/abortMultipartUpload.js +82 -0
- package/lib-esm/AwsClients/S3/base.d.ts +54 -0
- package/lib-esm/AwsClients/S3/base.js +71 -0
- package/lib-esm/AwsClients/S3/completeMultipartUpload.d.ts +20 -0
- package/lib-esm/AwsClients/S3/completeMultipartUpload.js +159 -0
- package/lib-esm/AwsClients/S3/copyObject.d.ts +24 -0
- package/lib-esm/AwsClients/S3/copyObject.js +95 -0
- package/lib-esm/AwsClients/S3/createMultipartUpload.d.ts +21 -0
- package/lib-esm/AwsClients/S3/createMultipartUpload.js +92 -0
- package/lib-esm/AwsClients/S3/deleteObject.d.ts +20 -0
- package/lib-esm/AwsClients/S3/deleteObject.js +83 -0
- package/lib-esm/AwsClients/S3/getObject.d.ts +27 -0
- package/lib-esm/AwsClients/S3/getObject.js +212 -0
- package/lib-esm/AwsClients/S3/headObject.d.ts +20 -0
- package/lib-esm/AwsClients/S3/headObject.js +93 -0
- package/lib-esm/AwsClients/S3/index.d.ts +13 -0
- package/lib-esm/AwsClients/S3/index.js +14 -0
- package/lib-esm/AwsClients/S3/index.native.d.ts +14 -0
- package/lib-esm/AwsClients/S3/index.native.js +15 -0
- package/lib-esm/AwsClients/S3/listObjectsV2.d.ts +20 -0
- package/lib-esm/AwsClients/S3/listObjectsV2.js +143 -0
- package/lib-esm/AwsClients/S3/listParts.d.ts +20 -0
- package/lib-esm/AwsClients/S3/listParts.js +107 -0
- package/lib-esm/AwsClients/S3/putObject.d.ts +23 -0
- package/lib-esm/AwsClients/S3/putObject.js +92 -0
- package/lib-esm/AwsClients/S3/runtime/base64/index.browser.d.ts +2 -0
- package/lib-esm/AwsClients/S3/runtime/base64/index.browser.js +15 -0
- package/lib-esm/AwsClients/S3/runtime/base64/index.native.d.ts +2 -0
- package/lib-esm/AwsClients/S3/runtime/base64/index.native.js +12 -0
- package/lib-esm/AwsClients/S3/runtime/constants.d.ts +9 -0
- package/lib-esm/AwsClients/S3/runtime/constants.js +11 -0
- package/lib-esm/AwsClients/S3/runtime/contentSha256middleware.d.ts +9 -0
- package/lib-esm/AwsClients/S3/runtime/contentSha256middleware.js +66 -0
- package/lib-esm/AwsClients/S3/runtime/index.browser.d.ts +5 -0
- package/lib-esm/AwsClients/S3/runtime/index.browser.js +8 -0
- package/lib-esm/AwsClients/S3/runtime/index.d.ts +5 -0
- package/lib-esm/AwsClients/S3/runtime/index.js +9 -0
- package/lib-esm/AwsClients/S3/runtime/index.native.d.ts +5 -0
- package/lib-esm/AwsClients/S3/runtime/index.native.js +8 -0
- package/lib-esm/AwsClients/S3/runtime/s3TransferHandler/fetch.d.ts +8 -0
- package/lib-esm/AwsClients/S3/runtime/s3TransferHandler/fetch.js +12 -0
- package/lib-esm/AwsClients/S3/runtime/s3TransferHandler/xhr.d.ts +8 -0
- package/lib-esm/AwsClients/S3/runtime/s3TransferHandler/xhr.js +18 -0
- package/lib-esm/AwsClients/S3/runtime/xhrTransferHandler.d.ts +22 -0
- package/lib-esm/AwsClients/S3/runtime/xhrTransferHandler.js +196 -0
- package/lib-esm/AwsClients/S3/runtime/xmlParser/dom.d.ts +8 -0
- package/lib-esm/AwsClients/S3/runtime/xmlParser/dom.js +89 -0
- package/lib-esm/AwsClients/S3/runtime/xmlParser/pureJs.d.ts +10 -0
- package/lib-esm/AwsClients/S3/runtime/xmlParser/pureJs.js +54 -0
- package/lib-esm/AwsClients/S3/types.d.ts +2857 -0
- package/lib-esm/AwsClients/S3/types.js +3 -0
- package/lib-esm/AwsClients/S3/uploadPart.d.ts +20 -0
- package/lib-esm/AwsClients/S3/uploadPart.js +95 -0
- package/lib-esm/AwsClients/S3/utils/deserializeHelpers.d.ts +78 -0
- package/lib-esm/AwsClients/S3/utils/deserializeHelpers.js +144 -0
- package/lib-esm/AwsClients/S3/utils/index.d.ts +5 -0
- package/lib-esm/AwsClients/S3/utils/index.js +7 -0
- package/lib-esm/AwsClients/S3/utils/parsePayload.d.ts +3 -0
- package/lib-esm/AwsClients/S3/utils/parsePayload.js +91 -0
- package/lib-esm/AwsClients/S3/utils/serializeHelpers.d.ts +42 -0
- package/lib-esm/AwsClients/S3/utils/serializeHelpers.js +176 -0
- package/lib-esm/Storage.d.ts +5 -5
- package/lib-esm/Storage.js +54 -25
- package/lib-esm/common/MD5utils.d.ts +1 -0
- package/lib-esm/common/MD5utils.js +75 -0
- package/lib-esm/common/MD5utils.native.d.ts +1 -0
- package/lib-esm/common/MD5utils.native.js +79 -0
- package/lib-esm/common/S3ClientUtils.d.ts +24 -15
- package/lib-esm/common/S3ClientUtils.js +76 -93
- package/lib-esm/common/StorageConstants.d.ts +0 -1
- package/lib-esm/common/StorageConstants.js +2 -2
- package/lib-esm/common/StorageErrorStrings.js +2 -1
- package/lib-esm/common/StorageUtils.js +2 -1
- package/lib-esm/index.js +1 -1
- package/lib-esm/providers/AWSS3Provider.d.ts +4 -8
- package/lib-esm/providers/AWSS3Provider.js +162 -115
- package/lib-esm/providers/AWSS3ProviderManagedUpload.d.ts +7 -7
- package/lib-esm/providers/AWSS3ProviderManagedUpload.js +193 -88
- package/lib-esm/providers/AWSS3UploadTask.d.ts +8 -8
- package/lib-esm/providers/AWSS3UploadTask.js +202 -106
- package/lib-esm/providers/index.js +2 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types/AWSS3Provider.d.ts +62 -62
- package/lib-esm/types/AWSS3Provider.js +1 -1
- package/lib-esm/types/Provider.d.ts +1 -1
- package/lib-esm/types/Provider.js +1 -1
- package/lib-esm/types/Storage.d.ts +23 -23
- package/lib-esm/types/Storage.js +1 -1
- package/lib-esm/types/index.js +3 -1
- package/package.json +21 -26
- package/src/AwsClients/S3/abortMultipartUpload.ts +62 -0
- package/src/AwsClients/S3/base.ts +109 -0
- package/src/AwsClients/S3/completeMultipartUpload.ts +154 -0
- package/src/AwsClients/S3/copyObject.ts +90 -0
- package/src/AwsClients/S3/createMultipartUpload.ts +75 -0
- package/src/AwsClients/S3/deleteObject.ts +69 -0
- package/src/AwsClients/S3/getObject.ts +173 -0
- package/src/AwsClients/S3/headObject.ts +91 -0
- package/src/AwsClients/S3/index.native.ts +42 -0
- package/src/AwsClients/S3/index.ts +41 -0
- package/src/AwsClients/S3/listObjectsV2.ts +130 -0
- package/src/AwsClients/S3/listParts.ts +97 -0
- package/src/AwsClients/S3/putObject.ts +99 -0
- package/src/AwsClients/S3/runtime/base64/index.browser.ts +21 -0
- package/src/AwsClients/S3/runtime/base64/index.native.ts +16 -0
- package/src/AwsClients/S3/runtime/constants.ts +16 -0
- package/src/AwsClients/S3/runtime/contentSha256middleware.ts +29 -0
- package/src/AwsClients/S3/runtime/index.browser.ts +14 -0
- package/src/AwsClients/S3/runtime/index.native.ts +14 -0
- package/src/AwsClients/S3/runtime/index.ts +15 -0
- package/src/AwsClients/S3/runtime/s3TransferHandler/fetch.ts +23 -0
- package/src/AwsClients/S3/runtime/s3TransferHandler/xhr.ts +35 -0
- package/src/AwsClients/S3/runtime/xhrTransferHandler.ts +254 -0
- package/src/AwsClients/S3/runtime/xmlParser/dom.ts +92 -0
- package/src/AwsClients/S3/runtime/xmlParser/pureJs.ts +55 -0
- package/src/AwsClients/S3/types.ts +2877 -0
- package/src/AwsClients/S3/uploadPart.ts +88 -0
- package/src/AwsClients/S3/utils/deserializeHelpers.ts +136 -0
- package/src/AwsClients/S3/utils/index.ts +27 -0
- package/src/AwsClients/S3/utils/parsePayload.ts +45 -0
- package/src/AwsClients/S3/utils/serializeHelpers.ts +112 -0
- package/src/Storage.ts +31 -27
- package/src/common/MD5utils.native.ts +38 -0
- package/src/common/MD5utils.ts +33 -0
- package/src/common/S3ClientUtils.ts +57 -113
- package/src/common/StorageConstants.ts +2 -2
- package/src/common/StorageErrorStrings.ts +2 -0
- package/src/common/StorageUtils.ts +2 -0
- package/src/providers/AWSS3Provider.ts +80 -100
- package/src/providers/AWSS3ProviderManagedUpload.ts +74 -64
- package/src/providers/AWSS3UploadTask.ts +86 -87
- package/src/providers/index.ts +2 -0
- package/src/types/AWSS3Provider.ts +55 -49
- package/lib/.tsbuildinfo +0 -3
- package/lib/Storage.js.map +0 -1
- package/lib/common/S3ClientUtils.js.map +0 -1
- package/lib/common/StorageConstants.js.map +0 -1
- package/lib/common/StorageErrorStrings.js.map +0 -1
- package/lib/common/StorageUtils.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/providers/AWSS3Provider.js.map +0 -1
- package/lib/providers/AWSS3ProviderManagedUpload.js.map +0 -1
- package/lib/providers/AWSS3UploadTask.js.map +0 -1
- package/lib/providers/axios-http-handler.d.ts +0 -40
- package/lib/providers/axios-http-handler.js +0 -193
- package/lib/providers/axios-http-handler.js.map +0 -1
- package/lib/providers/index.js.map +0 -1
- package/lib/types/AWSS3Provider.js.map +0 -1
- package/lib/types/Provider.js.map +0 -1
- package/lib/types/Storage.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/Storage.js.map +0 -1
- package/lib-esm/common/S3ClientUtils.js.map +0 -1
- package/lib-esm/common/StorageConstants.js.map +0 -1
- package/lib-esm/common/StorageErrorStrings.js.map +0 -1
- package/lib-esm/common/StorageUtils.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/providers/AWSS3Provider.js.map +0 -1
- package/lib-esm/providers/AWSS3ProviderManagedUpload.js.map +0 -1
- package/lib-esm/providers/AWSS3UploadTask.js.map +0 -1
- package/lib-esm/providers/axios-http-handler.d.ts +0 -40
- package/lib-esm/providers/axios-http-handler.js +0 -191
- package/lib-esm/providers/axios-http-handler.js.map +0 -1
- package/lib-esm/providers/index.js.map +0 -1
- package/lib-esm/types/AWSS3Provider.js.map +0 -1
- package/lib-esm/types/Provider.js.map +0 -1
- package/lib-esm/types/Storage.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
- package/src/providers/axios-http-handler.ts +0 -242
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Buffer } from 'buffer';
|
|
5
|
+
|
|
6
|
+
export function utf8Encode(input: string): Uint8Array {
|
|
7
|
+
return Buffer.from(input, 'utf-8');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function toBase64(input: string | ArrayBufferView): string {
|
|
11
|
+
if (typeof input === 'string') {
|
|
12
|
+
return Buffer.from(input, 'utf-8').toString('base64');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return new Buffer(input.buffer).toString('base64');
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export const SEND_UPLOAD_PROGRESS_EVENT = 'sendUploadProgress';
|
|
5
|
+
export const SEND_DOWNLOAD_PROGRESS_EVENT = 'sendDownloadProgress';
|
|
6
|
+
|
|
7
|
+
export const NETWORK_ERROR_MESSAGE = 'Network Error';
|
|
8
|
+
export const NETWORK_ERROR_CODE = 'ECONNABORTED';
|
|
9
|
+
|
|
10
|
+
export const ABORT_ERROR_MESSAGE = 'Request aborted';
|
|
11
|
+
export const ABORT_ERROR_CODE = 'ERR_ABORTED';
|
|
12
|
+
|
|
13
|
+
export const CANCELED_ERROR_MESSAGE = 'canceled';
|
|
14
|
+
export const CANCELED_ERROR_CODE = 'ERR_CANCELED';
|
|
15
|
+
|
|
16
|
+
export const CONTENT_SHA256_HEADER = 'x-amz-content-sha256';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
HttpRequest,
|
|
6
|
+
getHashedPayload,
|
|
7
|
+
MiddlewareHandler,
|
|
8
|
+
HttpResponse,
|
|
9
|
+
} from '@aws-amplify/core/internals/aws-client-utils';
|
|
10
|
+
import { CONTENT_SHA256_HEADER } from './constants';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A middleware that adds the x-amz-content-sha256 header to the request if it is not already present.
|
|
14
|
+
* It's required for S3 requests in browsers where the request body is sent in 1 chunk.
|
|
15
|
+
* @see https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export const contentSha256Middleware =
|
|
20
|
+
(options: {}) => (next: MiddlewareHandler<HttpRequest, HttpResponse>) =>
|
|
21
|
+
async function contentSha256Middleware(request: HttpRequest) {
|
|
22
|
+
if (request.headers[CONTENT_SHA256_HEADER]) {
|
|
23
|
+
return next(request);
|
|
24
|
+
} else {
|
|
25
|
+
const hash = await getHashedPayload(request.body);
|
|
26
|
+
request.headers[CONTENT_SHA256_HEADER] = hash;
|
|
27
|
+
return next(request);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Entry point for browser-specific S3 client utilities. It's used where DOMParser is available.
|
|
5
|
+
export {
|
|
6
|
+
SEND_DOWNLOAD_PROGRESS_EVENT,
|
|
7
|
+
SEND_UPLOAD_PROGRESS_EVENT,
|
|
8
|
+
CANCELED_ERROR_MESSAGE,
|
|
9
|
+
CONTENT_SHA256_HEADER,
|
|
10
|
+
} from './constants';
|
|
11
|
+
export { s3TransferHandler } from './s3TransferHandler/xhr';
|
|
12
|
+
export { parser } from './xmlParser/dom';
|
|
13
|
+
export { isCancelError } from './xhrTransferHandler';
|
|
14
|
+
export { toBase64, utf8Encode } from './base64/index.browser';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Entry point for ReactNative-specific S3 client utilities
|
|
5
|
+
export {
|
|
6
|
+
SEND_DOWNLOAD_PROGRESS_EVENT,
|
|
7
|
+
SEND_UPLOAD_PROGRESS_EVENT,
|
|
8
|
+
CANCELED_ERROR_MESSAGE,
|
|
9
|
+
CONTENT_SHA256_HEADER,
|
|
10
|
+
} from './constants';
|
|
11
|
+
export { s3TransferHandler } from './s3TransferHandler/xhr';
|
|
12
|
+
export { parser } from './xmlParser/pureJs';
|
|
13
|
+
export { isCancelError } from './xhrTransferHandler';
|
|
14
|
+
export { toBase64, utf8Encode } from './base64/index.native';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Entry point for Node.js-specific S3 client utilities
|
|
5
|
+
// This behavior is not guaranteed in v5.
|
|
6
|
+
export {
|
|
7
|
+
SEND_DOWNLOAD_PROGRESS_EVENT,
|
|
8
|
+
SEND_UPLOAD_PROGRESS_EVENT,
|
|
9
|
+
CANCELED_ERROR_MESSAGE,
|
|
10
|
+
CONTENT_SHA256_HEADER,
|
|
11
|
+
} from './constants';
|
|
12
|
+
export { s3TransferHandler } from './s3TransferHandler/fetch';
|
|
13
|
+
export { parser } from './xmlParser/pureJs';
|
|
14
|
+
export { isCancelError } from './xhrTransferHandler';
|
|
15
|
+
export { toBase64, utf8Encode } from './index.native';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
HttpRequest,
|
|
6
|
+
HttpResponse,
|
|
7
|
+
authenticatedHandler,
|
|
8
|
+
} from '@aws-amplify/core/internals/aws-client-utils';
|
|
9
|
+
import type { s3TransferHandler as s3BrowserTransferhandler } from './xhr';
|
|
10
|
+
import { composeTransferHandler } from '@aws-amplify/core/internals/aws-client-utils/composers';
|
|
11
|
+
import { contentSha256Middleware } from '../contentSha256middleware';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* S3 transfer handler for node based on Node-fetch. On top of basic transfer handler, it also supports
|
|
15
|
+
* x-amz-content-sha256 header. However, it does not support request&response process tracking like browser.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export const s3TransferHandler: typeof s3BrowserTransferhandler =
|
|
20
|
+
composeTransferHandler<[{}], HttpRequest, HttpResponse>(
|
|
21
|
+
authenticatedHandler,
|
|
22
|
+
[contentSha256Middleware]
|
|
23
|
+
);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
retryMiddleware,
|
|
6
|
+
RetryOptions,
|
|
7
|
+
signingMiddleware,
|
|
8
|
+
SigningOptions,
|
|
9
|
+
userAgentMiddleware,
|
|
10
|
+
UserAgentOptions,
|
|
11
|
+
HttpRequest,
|
|
12
|
+
HttpResponse,
|
|
13
|
+
} from '@aws-amplify/core/internals/aws-client-utils';
|
|
14
|
+
import { composeTransferHandler } from '@aws-amplify/core/internals/aws-client-utils/composers';
|
|
15
|
+
|
|
16
|
+
import { contentSha256Middleware } from '../contentSha256middleware';
|
|
17
|
+
import { xhrTransferHandler } from '../xhrTransferHandler';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* S3 transfer handler for browser and React Native based on XHR. On top of basic transfer handler, it also supports
|
|
21
|
+
* x-amz-content-sha256 header, and request&response process tracking.
|
|
22
|
+
*
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export const s3TransferHandler = composeTransferHandler<
|
|
26
|
+
[{}, UserAgentOptions, RetryOptions<HttpResponse>, SigningOptions],
|
|
27
|
+
HttpRequest,
|
|
28
|
+
HttpResponse,
|
|
29
|
+
typeof xhrTransferHandler
|
|
30
|
+
>(xhrTransferHandler, [
|
|
31
|
+
contentSha256Middleware,
|
|
32
|
+
userAgentMiddleware,
|
|
33
|
+
retryMiddleware,
|
|
34
|
+
signingMiddleware,
|
|
35
|
+
]);
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
HttpRequest,
|
|
6
|
+
HttpResponse,
|
|
7
|
+
TransferHandler,
|
|
8
|
+
ResponseBodyMixin,
|
|
9
|
+
withMemoization,
|
|
10
|
+
} from '@aws-amplify/core/internals/aws-client-utils';
|
|
11
|
+
import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
12
|
+
import type { EventEmitter } from 'events';
|
|
13
|
+
import {
|
|
14
|
+
SEND_DOWNLOAD_PROGRESS_EVENT,
|
|
15
|
+
SEND_UPLOAD_PROGRESS_EVENT,
|
|
16
|
+
ABORT_ERROR_CODE,
|
|
17
|
+
ABORT_ERROR_MESSAGE,
|
|
18
|
+
CANCELED_ERROR_CODE,
|
|
19
|
+
CANCELED_ERROR_MESSAGE,
|
|
20
|
+
NETWORK_ERROR_CODE,
|
|
21
|
+
NETWORK_ERROR_MESSAGE,
|
|
22
|
+
} from './constants';
|
|
23
|
+
|
|
24
|
+
const logger = new Logger('xhr-http-handler');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export interface XhrTransferHandlerOptions {
|
|
30
|
+
// Expected response body type. If `blob`, the response will be returned as a Blob object. It's mainly used to
|
|
31
|
+
// download binary data. Otherwise, use `text` to return the response as a string.
|
|
32
|
+
responseType: 'text' | 'blob';
|
|
33
|
+
abortSignal?: AbortSignal;
|
|
34
|
+
emitter?: EventEmitter;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Base transfer handler implementation using XMLHttpRequest to support upload and download progress events.
|
|
39
|
+
*
|
|
40
|
+
* @param request - The request object.
|
|
41
|
+
* @param options - The request options.
|
|
42
|
+
* @returns A promise that will be resolved with the response object.
|
|
43
|
+
*
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export const xhrTransferHandler: TransferHandler<
|
|
47
|
+
HttpRequest,
|
|
48
|
+
HttpResponse,
|
|
49
|
+
XhrTransferHandlerOptions
|
|
50
|
+
> = (request, options): Promise<HttpResponse> => {
|
|
51
|
+
const { url, method, headers, body } = request;
|
|
52
|
+
const { emitter, responseType, abortSignal } = options;
|
|
53
|
+
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
let xhr: XMLHttpRequest | null = new XMLHttpRequest();
|
|
56
|
+
xhr.open(method.toUpperCase(), url.toString());
|
|
57
|
+
|
|
58
|
+
Object.entries(headers)
|
|
59
|
+
.filter(([header]) => !FORBIDDEN_HEADERS.includes(header))
|
|
60
|
+
.forEach(([header, value]) => {
|
|
61
|
+
xhr!.setRequestHeader(header, value);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
xhr.responseType = responseType;
|
|
65
|
+
|
|
66
|
+
if (emitter) {
|
|
67
|
+
xhr.upload.addEventListener('progress', event => {
|
|
68
|
+
emitter.emit(SEND_UPLOAD_PROGRESS_EVENT, event);
|
|
69
|
+
logger.debug(event);
|
|
70
|
+
});
|
|
71
|
+
xhr.addEventListener('progress', event => {
|
|
72
|
+
emitter.emit(SEND_DOWNLOAD_PROGRESS_EVENT, event);
|
|
73
|
+
logger.debug(event);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
xhr.addEventListener('error', () => {
|
|
78
|
+
const error = simulateAxiosError(
|
|
79
|
+
NETWORK_ERROR_MESSAGE,
|
|
80
|
+
NETWORK_ERROR_CODE,
|
|
81
|
+
xhr!,
|
|
82
|
+
options
|
|
83
|
+
);
|
|
84
|
+
logger.error(NETWORK_ERROR_MESSAGE);
|
|
85
|
+
reject(error);
|
|
86
|
+
xhr = null; // clean up request
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Handle browser request cancellation (as opposed to a manual cancellation)
|
|
90
|
+
xhr.addEventListener('abort', () => {
|
|
91
|
+
// The abort event can be triggered after the error or load event. So we need to check if the xhr is null.
|
|
92
|
+
if (!xhr || abortSignal?.aborted) return;
|
|
93
|
+
const error = simulateAxiosError(
|
|
94
|
+
ABORT_ERROR_MESSAGE,
|
|
95
|
+
ABORT_ERROR_CODE,
|
|
96
|
+
xhr,
|
|
97
|
+
options
|
|
98
|
+
);
|
|
99
|
+
logger.error(ABORT_ERROR_MESSAGE);
|
|
100
|
+
reject(error);
|
|
101
|
+
xhr = null; // clean up request
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Skip handling timeout error since we don't have a timeout
|
|
105
|
+
|
|
106
|
+
xhr.addEventListener('readystatechange', () => {
|
|
107
|
+
if (!xhr || xhr.readyState !== xhr.DONE) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const onloadend = () => {
|
|
112
|
+
// The load event is triggered after the error/abort/load event. So we need to check if the xhr is null.
|
|
113
|
+
if (!xhr) return;
|
|
114
|
+
const responseHeaders = convertResponseHeaders(
|
|
115
|
+
xhr.getAllResponseHeaders()
|
|
116
|
+
);
|
|
117
|
+
const responseType = xhr.responseType;
|
|
118
|
+
const responseBlob = xhr.response as Blob;
|
|
119
|
+
const responseText = responseType === 'text' ? xhr.responseText : '';
|
|
120
|
+
const bodyMixIn: ResponseBodyMixin = {
|
|
121
|
+
blob: () => Promise.resolve(responseBlob),
|
|
122
|
+
text: withMemoization(() =>
|
|
123
|
+
responseType === 'blob'
|
|
124
|
+
? readBlobAsText(responseBlob)
|
|
125
|
+
: Promise.resolve(responseText)
|
|
126
|
+
),
|
|
127
|
+
json: () =>
|
|
128
|
+
Promise.reject(
|
|
129
|
+
// S3 does not support JSON response. So fail-fast here with nicer error message.
|
|
130
|
+
new Error(
|
|
131
|
+
'Parsing response to JSON is not implemented. Please use response.text() instead.'
|
|
132
|
+
)
|
|
133
|
+
),
|
|
134
|
+
};
|
|
135
|
+
const response: HttpResponse = {
|
|
136
|
+
statusCode: xhr.status,
|
|
137
|
+
headers: responseHeaders,
|
|
138
|
+
// The xhr.responseType is only set to 'blob' for streaming binary S3 object data. The streaming data is
|
|
139
|
+
// exposed via public interface of Storage.get(). So we need to return the response as a Blob object for
|
|
140
|
+
// backward compatibility. In other cases, the response payload is only used internally, we return it is
|
|
141
|
+
// {@link ResponseBodyMixin}
|
|
142
|
+
body: (xhr.responseType === 'blob'
|
|
143
|
+
? Object.assign(responseBlob, bodyMixIn)
|
|
144
|
+
: bodyMixIn) as HttpResponse['body'],
|
|
145
|
+
};
|
|
146
|
+
resolve(response);
|
|
147
|
+
xhr = null; // clean up request
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// readystate handler is calling before onerror or ontimeout handlers,
|
|
151
|
+
// so we should call onloadend on the next 'tick'
|
|
152
|
+
// @see https://github.com/axios/axios/blob/9588fcdec8aca45c3ba2f7968988a5d03f23168c/lib/adapters/xhr.js#L98-L99
|
|
153
|
+
setTimeout(onloadend);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (abortSignal) {
|
|
157
|
+
const onCancelled = () => {
|
|
158
|
+
// The abort event is triggered after the error or load event. So we need to check if the xhr is null.
|
|
159
|
+
if (!xhr) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const canceledError = simulateAxiosCanceledError(
|
|
163
|
+
CANCELED_ERROR_MESSAGE ?? abortSignal.reason,
|
|
164
|
+
CANCELED_ERROR_CODE,
|
|
165
|
+
xhr,
|
|
166
|
+
options
|
|
167
|
+
);
|
|
168
|
+
xhr.abort();
|
|
169
|
+
reject(canceledError);
|
|
170
|
+
xhr = null;
|
|
171
|
+
};
|
|
172
|
+
abortSignal.aborted
|
|
173
|
+
? onCancelled()
|
|
174
|
+
: abortSignal.addEventListener('abort', onCancelled);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (
|
|
178
|
+
typeof ReadableStream === 'function' &&
|
|
179
|
+
body instanceof ReadableStream
|
|
180
|
+
) {
|
|
181
|
+
// This does not matter as previous implementation uses Axios which does not support ReadableStream anyway.
|
|
182
|
+
throw new Error('ReadableStream request payload is not supported.');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
xhr.send((body as Exclude<BodyInit, ReadableStream>) ?? null);
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// TODO: V6 remove this
|
|
190
|
+
const simulateAxiosError = (
|
|
191
|
+
message: string,
|
|
192
|
+
code: string,
|
|
193
|
+
request: XMLHttpRequest,
|
|
194
|
+
config: XhrTransferHandlerOptions
|
|
195
|
+
) =>
|
|
196
|
+
Object.assign(new Error(message), {
|
|
197
|
+
code,
|
|
198
|
+
config,
|
|
199
|
+
request,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const simulateAxiosCanceledError = (
|
|
203
|
+
message: string,
|
|
204
|
+
code: string,
|
|
205
|
+
request: XMLHttpRequest,
|
|
206
|
+
config: XhrTransferHandlerOptions
|
|
207
|
+
) => {
|
|
208
|
+
const error = simulateAxiosError(message, code, request, config);
|
|
209
|
+
error.name = 'CanceledError';
|
|
210
|
+
error['__CANCEL__'] = true;
|
|
211
|
+
return error;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export const isCancelError = (error: unknown): boolean =>
|
|
215
|
+
!!error?.['__CANCEL__'];
|
|
216
|
+
/**
|
|
217
|
+
* Convert xhr.getAllResponseHeaders() string to a Record<string, string>. Note that modern browser already returns
|
|
218
|
+
* header names in lowercase.
|
|
219
|
+
* @param xhrHeaders - string of headers returned from xhr.getAllResponseHeaders()
|
|
220
|
+
*/
|
|
221
|
+
const convertResponseHeaders = (xhrHeaders: string): Record<string, string> => {
|
|
222
|
+
if (!xhrHeaders) {
|
|
223
|
+
return {};
|
|
224
|
+
}
|
|
225
|
+
return xhrHeaders
|
|
226
|
+
.split('\r\n')
|
|
227
|
+
.reduce((headerMap: Record<string, string>, line: string) => {
|
|
228
|
+
const parts = line.split(': ');
|
|
229
|
+
const header = parts.shift()!;
|
|
230
|
+
const value = parts.join(': ');
|
|
231
|
+
headerMap[header.toLowerCase()] = value;
|
|
232
|
+
return headerMap;
|
|
233
|
+
}, {});
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const readBlobAsText = (blob: Blob) => {
|
|
237
|
+
const reader = new FileReader();
|
|
238
|
+
return new Promise<string>((resolve, reject) => {
|
|
239
|
+
reader.onloadend = () => {
|
|
240
|
+
if (reader.readyState !== FileReader.DONE) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
resolve(reader.result as string);
|
|
244
|
+
};
|
|
245
|
+
reader.onerror = () => {
|
|
246
|
+
reject(reader.error);
|
|
247
|
+
};
|
|
248
|
+
reader.readAsText(blob);
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// To add more forbidden headers as found set by S3. Intentionally NOT list all of them here to save bundle size.
|
|
253
|
+
// https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
|
|
254
|
+
const FORBIDDEN_HEADERS = ['host'];
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Drop-in replacement for fast-xml-parser's XmlParser class used in the AWS SDK S3 client XML deserializer. This
|
|
6
|
+
* implementation is not tested against the full xml conformance test suite. It is only tested against the XML responses
|
|
7
|
+
* from S3. This implementation requires the `DOMParser` class in the runtime.
|
|
8
|
+
*/
|
|
9
|
+
export const parser = {
|
|
10
|
+
parse: (xmlStr: string): any => {
|
|
11
|
+
const domParser = new DOMParser();
|
|
12
|
+
const xml = domParser.parseFromString(xmlStr, 'text/xml');
|
|
13
|
+
const parsedObj = parseXmlNode(xml);
|
|
14
|
+
const rootKey = Object.keys(parsedObj)[0];
|
|
15
|
+
return parsedObj[rootKey];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const parseXmlNode = (node: Node): any => {
|
|
20
|
+
if (isDocumentNode(node)) {
|
|
21
|
+
return {
|
|
22
|
+
[node.documentElement.nodeName]: parseXmlNode(node.documentElement),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
27
|
+
return node.nodeValue?.trim();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isElementNode(node)) {
|
|
31
|
+
// Node like <Location>foo</Location> will be converted to { Location: 'foo' }
|
|
32
|
+
// instead of { Location: { '#text': 'foo' } }.
|
|
33
|
+
if (isTextOnlyElementNode(node)) {
|
|
34
|
+
return node.childNodes[0]?.nodeValue!;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const nodeValue = {};
|
|
38
|
+
// convert attributes
|
|
39
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
40
|
+
const attr = node.attributes[i];
|
|
41
|
+
if (!isNamespaceAttributeName(attr.nodeName)) {
|
|
42
|
+
nodeValue[attr.nodeName] = attr.nodeValue!;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// convert child nodes
|
|
47
|
+
if (node.children.length > 0) {
|
|
48
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
49
|
+
const child = node.children[i];
|
|
50
|
+
const childValue = parseXmlNode(child);
|
|
51
|
+
if (childValue === undefined) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const childName = child.nodeName;
|
|
55
|
+
if (nodeValue[childName] === undefined) {
|
|
56
|
+
nodeValue[childName] = childValue;
|
|
57
|
+
} else if (Array.isArray(nodeValue[childName])) {
|
|
58
|
+
nodeValue[childName].push(childValue);
|
|
59
|
+
} else {
|
|
60
|
+
nodeValue[childName] = [nodeValue[childName], childValue];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Return empty element node as empty string instead of `{}`, which is the default behavior of fast-xml-parser.
|
|
66
|
+
return Object.keys(nodeValue).length === 0 ? '' : nodeValue;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const isElementNode = (node: Node): node is Element =>
|
|
71
|
+
node.nodeType === Node.ELEMENT_NODE;
|
|
72
|
+
|
|
73
|
+
const isDocumentNode = (node: Node): node is Document =>
|
|
74
|
+
node.nodeType === Node.DOCUMENT_NODE;
|
|
75
|
+
|
|
76
|
+
const isTextOnlyElementNode = (node: Element): boolean =>
|
|
77
|
+
hasOnlyNamespaceAttributes(node) &&
|
|
78
|
+
node.children.length === 0 &&
|
|
79
|
+
node.firstChild?.nodeType === Node.TEXT_NODE;
|
|
80
|
+
|
|
81
|
+
const hasOnlyNamespaceAttributes = (node: Element): boolean => {
|
|
82
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
83
|
+
const attr = node.attributes[i];
|
|
84
|
+
if (!isNamespaceAttributeName(attr.nodeName)) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const isNamespaceAttributeName = (name: string): boolean =>
|
|
92
|
+
name === 'xmlns' || name.startsWith('xmlns:');
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { XMLParser } from 'fast-xml-parser';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pure JS XML parser that can be used in Non-browser environments, like React Native and Node.js. This is the same
|
|
8
|
+
* XML parser implementation as used in AWS SDK S3 client. It depends on pure JavaScript XML parser library
|
|
9
|
+
* `fast-xml-parser`.
|
|
10
|
+
*
|
|
11
|
+
* Ref: https://github.com/aws/aws-sdk-js-v3/blob/1e806ba3f4a83c9e3eb0b41a3a7092da93826b8f/clients/client-s3/src/protocols/Aws_restXml.ts#L12938-L12959
|
|
12
|
+
*/
|
|
13
|
+
export const parser = {
|
|
14
|
+
parse: (xmlStr: string): any => {
|
|
15
|
+
const parser = new XMLParser({
|
|
16
|
+
attributeNamePrefix: '',
|
|
17
|
+
htmlEntities: true,
|
|
18
|
+
ignoreAttributes: false,
|
|
19
|
+
ignoreDeclaration: true,
|
|
20
|
+
parseTagValue: false,
|
|
21
|
+
trimValues: false,
|
|
22
|
+
removeNSPrefix: true,
|
|
23
|
+
tagValueProcessor: (_, val) =>
|
|
24
|
+
val.trim() === '' && val.includes('\n') ? '' : undefined,
|
|
25
|
+
});
|
|
26
|
+
parser.addEntity('#xD', '\r');
|
|
27
|
+
parser.addEntity('#10', '\n');
|
|
28
|
+
const parsedObj = parser.parse(xmlStr);
|
|
29
|
+
const textNodeName = '#text';
|
|
30
|
+
const key = Object.keys(parsedObj)[0];
|
|
31
|
+
const parsedObjToReturn = parsedObj[key];
|
|
32
|
+
if (parsedObjToReturn[textNodeName]) {
|
|
33
|
+
parsedObjToReturn[key] = parsedObjToReturn[textNodeName];
|
|
34
|
+
delete parsedObjToReturn[textNodeName];
|
|
35
|
+
}
|
|
36
|
+
return getValueFromTextNode(parsedObjToReturn);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Recursively parses object and populates value is node from "#text" key if it's available
|
|
42
|
+
*
|
|
43
|
+
* Ref: https://github.com/aws/aws-sdk-js-v3/blob/6b4bde6f338720abf28b931f8a4506613bd64d3f/packages/smithy-client/src/get-value-from-text-node.ts#L1
|
|
44
|
+
*/
|
|
45
|
+
const getValueFromTextNode = (obj: any) => {
|
|
46
|
+
const textNodeName = '#text';
|
|
47
|
+
for (const key in obj) {
|
|
48
|
+
if (obj.hasOwnProperty(key) && obj[key][textNodeName] !== undefined) {
|
|
49
|
+
obj[key] = obj[key][textNodeName];
|
|
50
|
+
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
51
|
+
obj[key] = getValueFromTextNode(obj[key]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return obj;
|
|
55
|
+
};
|