import { config , Credentials } from 'aws-sdk/global' ; import { request as httpRequest , ClientRequest , ClientRequestArgs } from 'http' ; import { request as httpsRequest } from 'https' ; import { sign } from 'aws4' ; import { ClientOptions , Connection , Transport } from '@elastic/elasticsearch' ; import { ApiResponse , TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport' ; function generateAWSConnectionClass ( credentials ) { return class AWSConnection extends Connection { public constructor ( opts ) { super ( opts ) ; this . makeRequest = this . signedRequest ; } private signedRequest ( reqParams ) { const request = reqParams ?. protocol === 'https:' ? httpsRequest : httpRequest ; return request ( sign ( { ... reqParams , service : 'es' } , credentials ) ) ; } } ; } function generateAWSTransportClass ( credentials ) { return class AWSTransport extends Transport { public request ( params , options , callback = undefined ) { if ( typeof options === 'function' ) { callback = options ; options = { } ; } if ( typeof params === 'function' || params == null ) { callback = params ; params = { } ; options = { } ; } const isPromiseCall = typeof callback !== 'function' ; if ( isPromiseCall ) { return credentials . getPromise ( ) . then ( ( ) => super . request ( params , options , callback ) ) as TransportRequestPromise < ApiResponse < Record < string , any > , Record < string , unknown > > > ; } credentials . get ( err => { if ( err ) { callback ( err , null ) ; return ; } return super . request ( params , options , callback ) ; } ) ; } } ; } export function createAWSConnection ( awsCredentials ) { return { Connection : generateAWSConnectionClass ( awsCredentials ) , Transport : generateAWSTransportClass ( awsCredentials ) } ; } export const awsGetCredentials = ( ) : Promise < Credentials > => { return new Promise ( ( resolve , reject ) => { config . getCredentials ( err => { if ( err ) { return reject ( err ) ; } resolve ( config . credentials as Credentials ) ; } ) ; } ) ; } ;