UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

1 lines 6.24 kB
{"version":3,"file":"base.mjs","sources":["../../../../../../../src/providers/s3/utils/client/s3data/base.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { AmplifyUrl, getAmplifyUserAgent, } from '@aws-amplify/core/internals/utils';\nimport { getDnsSuffix, jitteredBackoff, } from '@aws-amplify/core/internals/aws-client-utils';\nimport { createRetryDecider, createXmlErrorParser } from '../utils';\nimport { LOCAL_TESTING_S3_ENDPOINT } from '../../constants';\nimport { assertValidationError } from '../../../../../errors/utils/assertValidationError';\nimport { StorageValidationErrorCode } from '../../../../../errors/types/validation';\nconst DOMAIN_PATTERN = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;\nconst IP_ADDRESS_PATTERN = /(\\d+\\.){3}\\d+/;\nconst DOTS_PATTERN = /\\.\\./;\n/**\n * The service name used to sign requests if the API requires authentication.\n */\nexport const SERVICE_NAME = 's3';\n/**\n * The endpoint resolver function that returns the endpoint URL for a given region, and input parameters.\n */\nconst endpointResolver = (options, apiInput) => {\n const { region, useAccelerateEndpoint, customEndpoint, forcePathStyle } = options;\n let endpoint;\n // 1. get base endpoint\n if (customEndpoint) {\n if (customEndpoint === LOCAL_TESTING_S3_ENDPOINT) {\n endpoint = new AmplifyUrl(customEndpoint);\n }\n assertValidationError(!customEndpoint.includes('://'), StorageValidationErrorCode.InvalidCustomEndpoint);\n endpoint = new AmplifyUrl(`https://${customEndpoint}`);\n }\n else if (useAccelerateEndpoint) {\n // this ErrorCode isn't expose yet since forcePathStyle param isn't publicly exposed\n assertValidationError(!forcePathStyle, StorageValidationErrorCode.ForcePathStyleEndpointNotSupported);\n endpoint = new AmplifyUrl(`https://s3-accelerate.${getDnsSuffix(region)}`);\n }\n else {\n endpoint = new AmplifyUrl(`https://s3.${region}.${getDnsSuffix(region)}`);\n }\n // 2. inject bucket name\n if (apiInput?.Bucket) {\n assertValidationError(isDnsCompatibleBucketName(apiInput.Bucket), StorageValidationErrorCode.DnsIncompatibleBucketName);\n if (forcePathStyle || apiInput.Bucket.includes('.')) {\n endpoint.pathname = `/${apiInput.Bucket}`;\n }\n else {\n endpoint.host = `${apiInput.Bucket}.${endpoint.host}`;\n }\n }\n return { url: endpoint };\n};\n/**\n * Determines whether a given string is DNS compliant per the rules outlined by\n * S3. Length, capitaization, and leading dot restrictions are enforced by the\n * DOMAIN_PATTERN regular expression.\n * @internal\n *\n * @see https://github.com/aws/aws-sdk-js-v3/blob/f2da6182298d4d6b02e84fb723492c07c27469a8/packages/middleware-bucket-endpoint/src/bucketHostnameUtils.ts#L39-L48\n * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html\n */\nexport const isDnsCompatibleBucketName = (bucketName) => DOMAIN_PATTERN.test(bucketName) &&\n !IP_ADDRESS_PATTERN.test(bucketName) &&\n !DOTS_PATTERN.test(bucketName);\n/**\n * Error parser for the XML payload of S3 data plane error response. The error's\n * `Code` and `Message` locates directly at the XML root element.\n *\n * @example\n * ```\n * <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n * \t<Error>\n * \t\t<Code>NoSuchKey</Code>\n * \t\t<Message>The resource you requested does not exist</Message>\n * \t\t<Resource>/mybucket/myfoto.jpg</Resource>\n * \t\t<RequestId>4442587FB7D0A2F9</RequestId>\n * \t</Error>\n * \t```\n *\n * @internal\n */\nexport const parseXmlError = createXmlErrorParser({ noErrorWrapping: true });\n/**\n * @internal\n */\nexport const retryDecider = createRetryDecider(parseXmlError);\n/**\n * @internal\n */\nexport const defaultConfig = {\n service: SERVICE_NAME,\n endpointResolver,\n retryDecider,\n computeDelay: jitteredBackoff,\n userAgentValue: getAmplifyUserAgent(),\n useAccelerateEndpoint: false,\n uriEscapePath: false, // Required by S3. See https://github.com/aws/aws-sdk-js-v3/blob/9ba012dfa3a3429aa2db0f90b3b0b3a7a31f9bc3/packages/signature-v4/src/SignatureV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