-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathawsSdkUtils.ts
107 lines (99 loc) · 3.22 KB
/
awsSdkUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import type { MiddlewareArgsLike, SdkClient } from './types/awsSdk.js';
import { PT_VERSION } from './version.js';
const EXEC_ENV = process.env.AWS_EXECUTION_ENV || 'NA';
const middlewareOptions = {
relation: 'after',
toMiddleware: 'getUserAgentMiddleware',
name: 'addPowertoolsToUserAgent',
tags: ['POWERTOOLS', 'USER_AGENT'],
};
/**
* Type guard to check if the client provided is a valid AWS SDK v3 client.
*
* @internal
*/
const isSdkClient = (client: unknown): client is SdkClient =>
typeof client === 'object' &&
client !== null &&
'send' in client &&
typeof client.send === 'function' &&
'config' in client &&
client.config !== undefined &&
typeof client.config === 'object' &&
client.config !== null &&
'middlewareStack' in client &&
client.middlewareStack !== undefined &&
typeof client.middlewareStack === 'object' &&
client.middlewareStack !== null &&
'identify' in client.middlewareStack &&
typeof client.middlewareStack.identify === 'function' &&
'addRelativeTo' in client.middlewareStack &&
typeof client.middlewareStack.addRelativeTo === 'function';
/**
* Helper function to create a custom user agent middleware for the AWS SDK v3 clients.
*
* The middleware will append the provided feature name and the current version of
* the Powertools for AWS Lambda library to the user agent string.
*
* @example "PT/Tracer/2.1.0 PTEnv/nodejs20x"
*
* @param feature The feature name to be added to the user agent
*
* @internal
*/
const customUserAgentMiddleware = (feature: string) => {
return <T extends MiddlewareArgsLike>(next: (arg0: T) => Promise<T>) =>
async (args: T) => {
const powertoolsUserAgent = `PT/${feature}/${PT_VERSION} PTEnv/${EXEC_ENV}`;
args.request.headers['user-agent'] =
`${args.request.headers['user-agent']} ${powertoolsUserAgent}`;
return await next(args);
};
};
/**
* Check if the provided middleware stack already has the Powertools for AWS Lambda
* user agent middleware.
*
* @param middlewareStack The middleware stack to check
*
* @internal
*/
const hasPowertools = (middlewareStack: string[]): boolean => {
let found = false;
for (const middleware of middlewareStack) {
if (middleware.includes('addPowertoolsToUserAgent')) {
found = true;
}
}
return found;
};
/**
* Add the Powertools for AWS Lambda user agent middleware to the
* AWS SDK v3 client provided.
*
* We use this middleware to unbotrusively track the usage of the library
* and secure continued investment in the project.
*
* @param client The AWS SDK v3 client to add the middleware to
* @param feature The feature name to be added to the user agent
*/
const addUserAgentMiddleware = (client: unknown, feature: string): void => {
try {
if (isSdkClient(client)) {
if (hasPowertools(client.middlewareStack.identify())) {
return;
}
client.middlewareStack.addRelativeTo(
customUserAgentMiddleware(feature),
middlewareOptions
);
} else {
throw new Error(
'The client provided does not match the expected interface'
);
}
} catch (error) {
console.warn('Failed to add user agent middleware', error);
}
};
export { customUserAgentMiddleware, addUserAgentMiddleware, isSdkClient };