@@ -13,14 +13,13 @@ const d = require('d');
13
13
const path = require ( 'path' ) ;
14
14
const spawnExt = require ( 'child-process-ext/spawn' ) ;
15
15
const ServerlessError = require ( '../../serverless-error' ) ;
16
- const awsRequest = require ( '../../aws/request' ) ;
17
16
const { cfValue } = require ( '../../utils/aws-schema-get-cf-value' ) ;
18
17
const reportDeprecatedProperties = require ( '../../utils/report-deprecated-properties' ) ;
19
18
const deepSortObjectByKey = require ( '../../utils/deep-sort-object-by-key' ) ;
20
19
const { progress, log } = require ( '@serverless/utils/log' ) ;
21
20
22
- // AWS SDK v3 infrastructure
23
- const AWSClientFactory = require ( '../../aws/client-factory' ) ;
21
+ const AWSV2ClientFactory = require ( '../../aws/v2-client-factory' ) ;
22
+ const AWSV3ClientFactory = require ( '../../aws/v3- client-factory' ) ;
24
23
const { createCommand } = require ( '../../aws/commands' ) ;
25
24
const { buildClientConfig, shouldUseS3Acceleration } = require ( '../../aws/config' ) ;
26
25
const { transformV3Error } = require ( '../../aws/error-utils' ) ;
@@ -1736,20 +1735,33 @@ class AwsProvider {
1736
1735
* @private
1737
1736
*/
1738
1737
async _requestV2 ( service , method , params , options ) {
1739
- // TODO: Determine calling module and log that
1740
1738
const requestOptions = _ . isObject ( options ) ? options : { } ;
1741
1739
const shouldCache = _ . get ( requestOptions , 'useCache' , false ) ;
1742
- // Copy is required as the credentials may be modified during the request
1743
- const credentials = Object . assign ( { } , this . getCredentials ( ) ) ;
1744
- const serviceOptions = {
1745
- name : service ,
1746
- params : {
1747
- ...credentials ,
1748
- region : _ . get ( requestOptions , 'region' , this . getRegion ( ) ) ,
1749
- isS3TransferAccelerationEnabled : this . isS3TransferAccelerationEnabled ( ) ,
1750
- } ,
1740
+ const region = _ . get ( requestOptions , 'region' , this . getRegion ( ) ) ;
1741
+
1742
+ // Initialize factory if needed
1743
+ if ( ! this . v2ClientFactory ) {
1744
+ this . v2ClientFactory = new AWSV2ClientFactory ( this . _getV2BaseConfig ( ) ) ;
1745
+ }
1746
+
1747
+ // Build client config (only configuration that affects client creation)
1748
+ const clientConfig = {
1749
+ ...this . getCredentials ( ) ,
1750
+ region,
1751
1751
} ;
1752
- return ( shouldCache ? awsRequest . memoized : awsRequest ) ( serviceOptions , method , params ) ;
1752
+
1753
+ // Handle S3 acceleration
1754
+ if ( service === 'S3' ) {
1755
+ const accelerationCompatibleMethods = new Set ( [ 'upload' , 'putObject' ] ) ;
1756
+ if ( accelerationCompatibleMethods . has ( method ) && this . isS3TransferAccelerationEnabled ( ) ) {
1757
+ clientConfig . useAccelerateEndpoint = true ;
1758
+ }
1759
+ }
1760
+
1761
+ // Use factory's request method which handles both client caching and response caching
1762
+ return this . v2ClientFactory . request ( service , method , params , clientConfig , {
1763
+ useCache : shouldCache ,
1764
+ } ) ;
1753
1765
}
1754
1766
1755
1767
/**
@@ -1764,8 +1776,8 @@ class AwsProvider {
1764
1776
async _requestV3 ( service , method , params = { } , options = { } ) {
1765
1777
try {
1766
1778
// Initialize client factory if not already done
1767
- if ( ! this . clientFactory ) {
1768
- this . clientFactory = new AWSClientFactory ( this . _getV3BaseConfig ( ) ) ;
1779
+ if ( ! this . v3ClientFactory ) {
1780
+ this . v3ClientFactory = new AWSV3ClientFactory ( this . _getV3BaseConfig ( ) ) ;
1769
1781
}
1770
1782
1771
1783
// Build client configuration
@@ -1775,7 +1787,7 @@ class AwsProvider {
1775
1787
const command = createCommand ( service , method , params ) ;
1776
1788
1777
1789
// Send request
1778
- const result = await this . clientFactory . send ( service , command , clientConfig ) ;
1790
+ const result = await this . v3ClientFactory . send ( service , command , clientConfig ) ;
1779
1791
1780
1792
return result ;
1781
1793
} catch ( error ) {
@@ -1791,12 +1803,12 @@ class AwsProvider {
1791
1803
* @returns {Object } AWS SDK v3 client instance
1792
1804
*/
1793
1805
getV3Client ( service , options = { } ) {
1794
- if ( ! this . clientFactory ) {
1795
- this . clientFactory = new AWSClientFactory ( this . _getV3BaseConfig ( ) ) ;
1806
+ if ( ! this . v3ClientFactory ) {
1807
+ this . v3ClientFactory = new AWSV3ClientFactory ( this . _getV3BaseConfig ( ) ) ;
1796
1808
}
1797
1809
1798
1810
const clientConfig = this . _buildV3ClientConfig ( service , null , options ) ;
1799
- return this . clientFactory . getClient ( service , clientConfig ) ;
1811
+ return this . v3ClientFactory . getClient ( service , clientConfig ) ;
1800
1812
}
1801
1813
1802
1814
/**
@@ -1837,6 +1849,72 @@ class AwsProvider {
1837
1849
return baseConfig ;
1838
1850
}
1839
1851
1852
+ /**
1853
+ * Build base configuration for AWS SDK v2 clients
1854
+ * @private
1855
+ */
1856
+ _getV2BaseConfig ( ) {
1857
+ const config = { } ;
1858
+
1859
+ // Handle proxy configuration
1860
+ const proxy =
1861
+ process . env . proxy ||
1862
+ process . env . HTTP_PROXY ||
1863
+ process . env . http_proxy ||
1864
+ process . env . HTTPS_PROXY ||
1865
+ process . env . https_proxy ;
1866
+
1867
+ const proxyOptions = { } ;
1868
+ if ( proxy ) {
1869
+ Object . assign ( proxyOptions , require ( 'url' ) . parse ( proxy ) ) ;
1870
+ }
1871
+
1872
+ // Handle CA certificates
1873
+ const ca = process . env . ca || process . env . HTTPS_CA || process . env . https_ca ;
1874
+ let caCerts = [ ] ;
1875
+
1876
+ if ( ca ) {
1877
+ const caArr = ca . split ( ',' ) ;
1878
+ caCerts = caCerts . concat ( caArr . map ( ( cert ) => cert . replace ( / \\ n / g, '\n' ) ) ) ;
1879
+ }
1880
+
1881
+ const cafile = process . env . cafile || process . env . HTTPS_CAFILE || process . env . https_cafile ;
1882
+ if ( cafile ) {
1883
+ const caPathArr = cafile . split ( ',' ) ;
1884
+ caCerts = caCerts . concat (
1885
+ caPathArr . map ( ( cafilePath ) => require ( 'fs' ) . readFileSync ( cafilePath . trim ( ) ) )
1886
+ ) ;
1887
+ }
1888
+
1889
+ if ( caCerts . length > 0 ) {
1890
+ Object . assign ( proxyOptions , {
1891
+ rejectUnauthorized : true ,
1892
+ ca : caCerts ,
1893
+ } ) ;
1894
+ }
1895
+
1896
+ // Set up HTTP options
1897
+ const httpOptions = { } ;
1898
+
1899
+ if ( proxy ) {
1900
+ httpOptions . agent = new ( require ( 'https-proxy-agent' ) ) ( proxyOptions ) ;
1901
+ } else if ( proxyOptions . ca ) {
1902
+ httpOptions . agent = new ( require ( 'https' ) . Agent ) ( proxyOptions ) ;
1903
+ }
1904
+
1905
+ // Handle timeout
1906
+ const timeout = process . env . AWS_CLIENT_TIMEOUT || process . env . aws_client_timeout ;
1907
+ if ( timeout ) {
1908
+ httpOptions . timeout = parseInt ( timeout , 10 ) ;
1909
+ }
1910
+
1911
+ if ( Object . keys ( httpOptions ) . length > 0 ) {
1912
+ config . httpOptions = httpOptions ;
1913
+ }
1914
+
1915
+ return config ;
1916
+ }
1917
+
1840
1918
/**
1841
1919
* Fetch credentials directly or using a profile from serverless yml configuration or from the
1842
1920
* well known environment variables
0 commit comments