Skip to content

Commit 3ef7dd1

Browse files
committed
fix: add middleware to inject http header
1 parent 4208153 commit 3ef7dd1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export class AWSClientBuilderV3 {
113113
service.middlewareStack.add(telemetryMiddleware, { step: 'deserialize' })
114114
service.middlewareStack.add(loggingMiddleware, { step: 'finalizeRequest' })
115115
service.middlewareStack.add(getEndpointMiddleware(settings), { step: 'build' })
116+
service.middlewareStack.add(keepAliveMiddleware, { step: 'build' })
116117
return service
117118
}
118119
}
@@ -155,6 +156,9 @@ function getEndpointMiddleware(settings: DevSettings = DevSettings.instance): Bu
155156
overwriteEndpoint(next, context, settings, args)
156157
}
157158

159+
const keepAliveMiddleware: BuildMiddleware<any, any> = (next: BuildHandler<any, any>) => async (args: any) =>
160+
addKeepAliveHeader(next, args)
161+
158162
export async function emitOnRequest(next: DeserializeHandler<any, any>, context: HandlerExecutionContext, args: any) {
159163
if (!HttpResponse.isInstance(args.request)) {
160164
return next(args)
@@ -176,8 +180,9 @@ export async function emitOnRequest(next: DeserializeHandler<any, any>, context:
176180
}
177181

178182
export async function logOnRequest(next: FinalizeHandler<any, any>, args: any) {
183+
const request = args.request
179184
if (HttpRequest.isInstance(args.request)) {
180-
const { hostname, path } = args.request
185+
const { hostname, path } = request
181186
// TODO: omit credentials / sensitive info from the logs.
182187
const input = partialClone(args.input, 3)
183188
getLogger().debug(`API Request (%s %s): %O`, hostname, path, input)
@@ -191,14 +196,23 @@ export function overwriteEndpoint(
191196
settings: DevSettings,
192197
args: any
193198
) {
194-
if (HttpRequest.isInstance(args.request)) {
195-
const serviceId = getServiceId(context as object)
199+
const request = args.request
200+
if (HttpRequest.isInstance(request)) {
201+
const serviceId = getServiceId(context satisfies { clientName?: string; commandName?: string })
196202
const endpoint = serviceId ? settings.get('endpoints', {})[serviceId] : undefined
197203
if (endpoint) {
198204
const url = new URL(endpoint)
199-
Object.assign(args.request, selectFrom(url, 'hostname', 'port', 'protocol', 'pathname'))
200-
args.request.path = args.request.pathname
205+
Object.assign(request, selectFrom(url, 'hostname', 'port', 'protocol', 'pathname'))
206+
request.path = (request as HttpRequest & { pathname: string }).pathname
201207
}
202208
}
203209
return next(args)
204210
}
211+
212+
export function addKeepAliveHeader(next: BuildHandler<any, any>, args: any) {
213+
const request = args.request
214+
if (HttpRequest.isInstance(request)) {
215+
request.headers['Connection'] = 'keep-alive'
216+
}
217+
return next(args)
218+
}

packages/core/src/testE2E/shared/awsClientBuilderV3.test.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@ import http from 'http'
77
import assert from 'assert'
88
import { SSMClient, DescribeSessionsCommand } from '@aws-sdk/client-ssm'
99
import { NodeHttpHandler } from '@smithy/node-http-handler'
10-
import { Logger } from '@aws-sdk/types'
1110
import { globals } from '../../shared'
1211

13-
describe('SDKv3 Client Behavior', function () {
14-
const port = 4576
12+
describe('AWSClientBuilderV3', function () {
13+
const port = 3000
1514
let server: http.Server
16-
let testLogger: Logger
1715
let requests: http.IncomingMessage[]
1816

1917
before(function () {
2018
server = http.createServer({ keepAlive: true }, (req, rsp) => {
2119
rsp.writeHead(200, { 'Content-Type': 'application/json' })
2220
rsp.end(JSON.stringify({ message: 'success' }))
2321
})
24-
const defaultLog = (m: object) => console.log(JSON.stringify(m, undefined, 3))
25-
testLogger = {
26-
debug: defaultLog,
27-
warn: defaultLog,
28-
error: defaultLog,
29-
info: defaultLog,
30-
}
3122
server.listen(port, () => {})
3223
server.on('request', (req) => {
3324
requests.push(req)
@@ -42,20 +33,20 @@ describe('SDKv3 Client Behavior', function () {
4233
server.close()
4334
})
4435

45-
it('reuses connections', async function () {
36+
it('reuses existing HTTP connections', async function () {
4637
const httpHandler = new NodeHttpHandler({
4738
httpAgent: new http.Agent({ keepAlive: true }),
4839
})
4940
const client = await globals.sdkClientBuilderV3.createAwsService(SSMClient, {
5041
region: 'us-east-1',
5142
endpoint: `http://localhost:${port}`,
5243
requestHandler: httpHandler,
53-
logger: testLogger,
5444
})
5545
await client.send(new DescribeSessionsCommand({ State: 'Active' }))
5646
await client.send(new DescribeSessionsCommand({ State: 'Active' }))
5747

5848
assert.strictEqual(requests[0].headers.connection, 'keep-alive')
5949
assert.strictEqual(requests[1].headers.connection, 'keep-alive')
50+
assert.strictEqual(server.connections, 1)
6051
})
6152
})

0 commit comments

Comments
 (0)