1818
1919import { GrpcService , GrpcServiceConfig } from './common-grpc/service' ;
2020import { PreciseDate } from '@google-cloud/precise-date' ;
21- import { replaceProjectIdToken } from './helper' ;
21+ import { hasProjectIdToken , replaceProjectIdToken } from './helper' ;
2222import { promisifyAll } from '@google-cloud/promisify' ;
2323import * as extend from 'extend' ;
2424import { GoogleAuth , GoogleAuthOptions } from 'google-auth-library' ;
@@ -342,6 +342,9 @@ class Spanner extends GrpcService {
342342 private _isInSecureCredentials : boolean ;
343343 private _metricsEnabled = false ;
344344 readonly _nthClientId : number ;
345+ private _pendingProjectIdCallbacks ?: Array <
346+ ( error : Error | null , projectId ?: string ) => void
347+ > ;
345348
346349 /**
347350 * Placeholder used to auto populate a column with the commit timestamp.
@@ -1735,54 +1738,138 @@ class Spanner extends GrpcService {
17351738 * @param {object } config Request config
17361739 * @param {function } callback Callback function
17371740 */
1738- prepareGapicRequest_ ( config , callback ) {
1739- this . auth . getProjectId ( ( err , projectId ) => {
1740- if ( err ) {
1741- callback ( err ) ;
1741+ prepareGapicRequest_ (
1742+ config : RequestConfig ,
1743+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1744+ callback : ( err : Error | null , requestFn ?: any ) => void ,
1745+ ) : void {
1746+ if ( this . projectId && this . projectIdReplaced_ ) {
1747+ this . _prepareGapicRequestWithProjectId ( config , this . projectId , callback ) ;
1748+ return ;
1749+ }
1750+ if ( this . _pendingProjectIdCallbacks ) {
1751+ this . _pendingProjectIdCallbacks . push ( ( error , projectId ) => {
1752+ if ( error ) {
1753+ callback ( error ) ;
1754+ return ;
1755+ }
1756+ this . _prepareGapicRequestWithProjectId ( config , projectId ! , callback ) ;
1757+ } ) ;
1758+ return ;
1759+ }
1760+ this . _pendingProjectIdCallbacks = [ ] ;
1761+ this . auth . getProjectId ( ( error , projectId ) => {
1762+ const pendingCallbacks = this . _pendingProjectIdCallbacks || [ ] ;
1763+ this . _pendingProjectIdCallbacks = undefined ;
1764+ if ( error ) {
1765+ try {
1766+ callback ( error ) ;
1767+ } finally {
1768+ for ( const pendingCallback of pendingCallbacks ) {
1769+ try {
1770+ pendingCallback ( error ) ;
1771+ } catch {
1772+ // Prevent one failing user callback from stranding subsequent pending callers.
1773+ }
1774+ }
1775+ }
17421776 return ;
17431777 }
1744- const clientName = config . client ;
17451778 try {
1746- if ( ! this . clients_ . has ( clientName ) ) {
1747- this . clients_ . set (
1748- clientName ,
1749- new ( v1 as Record < string , any > ) [ clientName ] ( this . options ) ,
1750- ) ;
1779+ this . _prepareGapicRequestWithProjectId ( config , projectId ! , callback ) ;
1780+ } finally {
1781+ for ( const pendingCallback of pendingCallbacks ) {
1782+ try {
1783+ pendingCallback ( null , projectId ! ) ;
1784+ } catch {
1785+ // Prevent one failing user callback from stranding subsequent pending callers.
1786+ }
17511787 }
1752- } catch ( err ) {
1753- callback ( err , null ) ;
1788+ }
1789+ } ) ;
1790+ }
1791+
1792+ private _prepareGapicRequestWithProjectId (
1793+ config : RequestConfig ,
1794+ projectId : string ,
1795+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1796+ callback : ( err : Error | null , requestFn ?: any ) => void ,
1797+ ) : void {
1798+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1799+ let wrappedRequestFn : any ;
1800+ try {
1801+ const clientName = config . client ;
1802+ if ( ! this . clients_ . has ( clientName ) ) {
1803+ this . clients_ . set (
1804+ clientName ,
1805+ new ( v1 as Record < string , any > ) [ clientName ] ( this . options ) ,
1806+ ) ;
17541807 }
17551808 const gaxClient = this . clients_ . get ( clientName ) ! ;
1756- let reqOpts = extend ( true , { } , config . reqOpts ) ;
1757- reqOpts = replaceProjectIdToken ( reqOpts , projectId ! ) ;
1758- // It would have been preferable to replace the projectId already in the
1759- // constructor of Spanner, but that is not possible as auth.getProjectId
1760- // is an async method. This is therefore the first place where we have
1761- // access to the value that should be used instead of the placeholder.
1809+ let reqOpts = config . reqOpts ;
1810+ if ( ! this . projectIdReplaced_ || hasProjectIdToken ( reqOpts ) ) {
1811+ reqOpts = extend ( true , { } , config . reqOpts ) ;
1812+ reqOpts = replaceProjectIdToken ( reqOpts , projectId ) ;
1813+ }
17621814 if ( ! this . projectIdReplaced_ ) {
1763- this . projectId = replaceProjectIdToken ( this . projectId , projectId ! ) ;
1815+ this . projectId = replaceProjectIdToken ( this . projectId , projectId ) ;
17641816 this . projectFormattedName_ = replaceProjectIdToken (
17651817 this . projectFormattedName_ ,
1766- projectId ! ,
1818+ projectId ,
17671819 ) ;
1820+ if (
1821+ this . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] ?. includes ( '{{projectId}}' )
1822+ ) {
1823+ this . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] = replaceProjectIdToken (
1824+ this . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] ,
1825+ projectId ,
1826+ ) ;
1827+ }
17681828 this . instances_ . forEach ( instance => {
17691829 instance . formattedName_ = replaceProjectIdToken (
17701830 instance . formattedName_ ,
1771- projectId ! ,
1831+ projectId ,
17721832 ) ;
1833+ if (
1834+ instance . commonHeaders_ ?. [ CLOUD_RESOURCE_HEADER ] ?. includes (
1835+ '{{projectId}}' ,
1836+ )
1837+ ) {
1838+ instance . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] =
1839+ replaceProjectIdToken (
1840+ instance . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] ,
1841+ projectId ,
1842+ ) ;
1843+ }
17731844 instance . databases_ . forEach ( database => {
17741845 database . formattedName_ = replaceProjectIdToken (
17751846 database . formattedName_ ,
1776- projectId ! ,
1847+ projectId ,
17771848 ) ;
1849+ if (
1850+ database . commonHeaders_ ?. [ CLOUD_RESOURCE_HEADER ] ?. includes (
1851+ '{{projectId}}' ,
1852+ )
1853+ ) {
1854+ database . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] =
1855+ replaceProjectIdToken (
1856+ database . commonHeaders_ [ CLOUD_RESOURCE_HEADER ] ,
1857+ projectId ,
1858+ ) ;
1859+ }
17781860 } ) ;
17791861 } ) ;
17801862 this . projectIdReplaced_ = true ;
17811863 }
1782- config . headers [ CLOUD_RESOURCE_HEADER ] = replaceProjectIdToken (
1783- config . headers [ CLOUD_RESOURCE_HEADER ] ,
1784- projectId ! ,
1785- ) ;
1864+ if ( ! config . headers ) {
1865+ config . headers = { } ;
1866+ }
1867+ if ( config . headers [ CLOUD_RESOURCE_HEADER ] ?. includes ( '{{projectId}}' ) ) {
1868+ config . headers [ CLOUD_RESOURCE_HEADER ] = replaceProjectIdToken (
1869+ config . headers [ CLOUD_RESOURCE_HEADER ] ,
1870+ projectId ,
1871+ ) ;
1872+ }
17861873 if ( isTracingEnabled ( this . _observabilityOptions ) ) {
17871874 // Do context propagation
17881875 propagation . inject ( context . active ( ) , config . headers , {
@@ -1793,22 +1880,39 @@ class Spanner extends GrpcService {
17931880 // Attach the x-goog-spanner-request-id to the currently active span.
17941881 attributeXGoogSpannerRequestIdToActiveSpan ( config ) ;
17951882 }
1796- const interceptors : any [ ] = [ ] ;
1797- if ( this . _metricsEnabled ) {
1798- interceptors . push ( MetricInterceptor ) ;
1883+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1884+ const customInterceptors : any [ ] =
1885+ config . gaxOpts ?. otherArgs ?. options ?. interceptors ?? [ ] ;
1886+ const interceptors = this . _metricsEnabled
1887+ ? [ ...customInterceptors , MetricInterceptor ]
1888+ : customInterceptors ;
1889+ const headers = Object . assign (
1890+ { } ,
1891+ config . gaxOpts ?. otherArgs ?. headers ,
1892+ config . headers ,
1893+ ) ;
1894+ const options = Object . assign ( { } , config . gaxOpts ?. otherArgs ?. options , {
1895+ interceptors,
1896+ } ) ;
1897+ // Deep clone caller-owned properties to prevent downstream mutations
1898+ // (e.g. gax mutating `retry.backoffSettings`), while attaching our
1899+ // freshly merged otherArgs without redundantly deep-cloning them.
1900+ let gaxOpts : CallOptions ;
1901+ if ( config . gaxOpts ) {
1902+ const { otherArgs : callerOtherArgs , ...restGaxOpts } = config . gaxOpts ;
1903+ gaxOpts = extend ( true , { } , restGaxOpts ) ;
1904+ gaxOpts . otherArgs = Object . assign ( { } , callerOtherArgs , {
1905+ headers,
1906+ options,
1907+ } ) ;
1908+ } else {
1909+ gaxOpts = { otherArgs : { headers, options} } ;
17991910 }
1911+
18001912 const requestFn = gaxClient [ config . method ] . bind (
18011913 gaxClient ,
18021914 reqOpts ,
1803- // Add headers to `gaxOpts`
1804- extend ( true , { } , config . gaxOpts , {
1805- otherArgs : {
1806- headers : config . headers ,
1807- options : {
1808- interceptors : interceptors ,
1809- } ,
1810- } ,
1811- } ) ,
1915+ gaxOpts ,
18121916 ) ;
18131917
18141918 // Extract a lightweight config reference containing only headers so error
@@ -1821,7 +1925,7 @@ class Spanner extends GrpcService {
18211925 //
18221926 // Because reqOpts and gaxOpts are already pre-bound to requestFn, wrappedRequestFn
18231927 // receives at most one argument: an optional callback.
1824- const wrappedRequestFn = ( callback ?: Function ) => {
1928+ wrappedRequestFn = ( callback ?: Function ) => {
18251929 // Callback mode: invoke requestFn with an intercepted callback to enrich
18261930 // the error parameter before delegating to the caller's callback.
18271931 if ( typeof callback === 'function' ) {
@@ -1876,9 +1980,12 @@ class Spanner extends GrpcService {
18761980
18771981 return res ;
18781982 } ;
1983+ } catch ( error ) {
1984+ callback ( error as Error , null ) ;
1985+ return ;
1986+ }
18791987
1880- callback ( null , wrappedRequestFn ) ;
1881- } ) ;
1988+ callback ( null , wrappedRequestFn ) ;
18821989 }
18831990
18841991 /**
@@ -1905,7 +2012,7 @@ class Spanner extends GrpcService {
19052012 MetricsTracerFactory ?. getInstance ( this . projectId_ ) ?. createMetricsTracer (
19062013 config . method ,
19072014 config . reqOpts . database ?? config . reqOpts . session ,
1908- config . headers [ 'x-goog-spanner-request-id' ] ,
2015+ config . headers ?. [ 'x-goog-spanner-request-id' ] ,
19092016 ) ?? null ;
19102017 }
19112018 metricsTracer ?. recordOperationStart ( ) ;
@@ -1975,7 +2082,7 @@ class Spanner extends GrpcService {
19752082 MetricsTracerFactory ?. getInstance ( this . projectId_ ) ?. createMetricsTracer (
19762083 config . method ,
19772084 config . reqOpts . session ?? config . reqOpts . database ,
1978- config . headers [ 'x-goog-spanner-request-id' ] ,
2085+ config . headers ?. [ 'x-goog-spanner-request-id' ] ,
19792086 ) ?? null ;
19802087 }
19812088 metricsTracer ?. recordOperationStart ( ) ;
0 commit comments