@aws-sdk/ec2-metadata-service 3.955.0 → 3.957.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-cjs/index.js CHANGED
@@ -42,6 +42,8 @@ class MetadataService {
42
42
  config;
43
43
  retries;
44
44
  backoffFn;
45
+ tokenTtl;
46
+ port;
45
47
  constructor(options = {}) {
46
48
  this.config = (async () => {
47
49
  const profile = options?.profile || process.env.AWS_PROFILE;
@@ -56,6 +58,23 @@ class MetadataService {
56
58
  this.disableFetchToken = options?.disableFetchToken || false;
57
59
  this.retries = options?.retries ?? 3;
58
60
  this.backoffFn = this.createBackoffFunction(options?.backoff);
61
+ this.tokenTtl = this.validateTokenTtl(options?.tokenTtl ?? 21600);
62
+ this.port = options?.port;
63
+ }
64
+ validateTokenTtl(tokenTtl) {
65
+ if (!Number.isInteger(tokenTtl) || tokenTtl <= 0) {
66
+ throw new Error("tokenTtl must be a positive integer");
67
+ }
68
+ return tokenTtl;
69
+ }
70
+ resolvePort(endpointUrl) {
71
+ if (this.port !== undefined) {
72
+ return this.port;
73
+ }
74
+ if (endpointUrl.port) {
75
+ return parseInt(endpointUrl.port);
76
+ }
77
+ return undefined;
59
78
  }
60
79
  createBackoffFunction(backoff) {
61
80
  if (typeof backoff === "function") {
@@ -127,7 +146,7 @@ class MetadataService {
127
146
  hostname: endpointUrl.hostname,
128
147
  path: endpointUrl.pathname + path,
129
148
  protocol: endpointUrl.protocol,
130
- port: endpointUrl.port ? parseInt(endpointUrl.port) : undefined,
149
+ port: this.resolvePort(endpointUrl),
131
150
  });
132
151
  try {
133
152
  const { response } = await handler.handle(request, {});
@@ -164,12 +183,12 @@ class MetadataService {
164
183
  const tokenRequest = new protocolHttp.HttpRequest({
165
184
  method: "PUT",
166
185
  headers: {
167
- "x-aws-ec2-metadata-token-ttl-seconds": "21600",
186
+ "x-aws-ec2-metadata-token-ttl-seconds": String(this.tokenTtl),
168
187
  },
169
188
  hostname: endpointUrl.hostname,
170
189
  path: "/latest/api/token",
171
190
  protocol: endpointUrl.protocol,
172
- port: endpointUrl.port ? parseInt(endpointUrl.port) : undefined,
191
+ port: this.resolvePort(endpointUrl),
173
192
  });
174
193
  try {
175
194
  const { response } = await handler.handle(tokenRequest, {});
@@ -8,6 +8,8 @@ export class MetadataService {
8
8
  config;
9
9
  retries;
10
10
  backoffFn;
11
+ tokenTtl;
12
+ port;
11
13
  constructor(options = {}) {
12
14
  this.config = (async () => {
13
15
  const profile = options?.profile || process.env.AWS_PROFILE;
@@ -22,6 +24,23 @@ export class MetadataService {
22
24
  this.disableFetchToken = options?.disableFetchToken || false;
23
25
  this.retries = options?.retries ?? 3;
24
26
  this.backoffFn = this.createBackoffFunction(options?.backoff);
27
+ this.tokenTtl = this.validateTokenTtl(options?.tokenTtl ?? 21600);
28
+ this.port = options?.port;
29
+ }
30
+ validateTokenTtl(tokenTtl) {
31
+ if (!Number.isInteger(tokenTtl) || tokenTtl <= 0) {
32
+ throw new Error("tokenTtl must be a positive integer");
33
+ }
34
+ return tokenTtl;
35
+ }
36
+ resolvePort(endpointUrl) {
37
+ if (this.port !== undefined) {
38
+ return this.port;
39
+ }
40
+ if (endpointUrl.port) {
41
+ return parseInt(endpointUrl.port);
42
+ }
43
+ return undefined;
25
44
  }
26
45
  createBackoffFunction(backoff) {
27
46
  if (typeof backoff === "function") {
@@ -93,7 +112,7 @@ export class MetadataService {
93
112
  hostname: endpointUrl.hostname,
94
113
  path: endpointUrl.pathname + path,
95
114
  protocol: endpointUrl.protocol,
96
- port: endpointUrl.port ? parseInt(endpointUrl.port) : undefined,
115
+ port: this.resolvePort(endpointUrl),
97
116
  });
98
117
  try {
99
118
  const { response } = await handler.handle(request, {});
@@ -130,12 +149,12 @@ export class MetadataService {
130
149
  const tokenRequest = new HttpRequest({
131
150
  method: "PUT",
132
151
  headers: {
133
- "x-aws-ec2-metadata-token-ttl-seconds": "21600",
152
+ "x-aws-ec2-metadata-token-ttl-seconds": String(this.tokenTtl),
134
153
  },
135
154
  hostname: endpointUrl.hostname,
136
155
  path: "/latest/api/token",
137
156
  protocol: endpointUrl.protocol,
138
- port: endpointUrl.port ? parseInt(endpointUrl.port) : undefined,
157
+ port: this.resolvePort(endpointUrl),
139
158
  });
140
159
  try {
141
160
  const { response } = await handler.handle(tokenRequest, {});
@@ -7,10 +7,14 @@ export declare class MetadataService {
7
7
  private config;
8
8
  private retries;
9
9
  private backoffFn;
10
+ private tokenTtl;
11
+ private port?;
10
12
  /**
11
13
  * Creates a new MetadataService object with a given set of options.
12
14
  */
13
15
  constructor(options?: MetadataServiceOptions);
16
+ private validateTokenTtl;
17
+ private resolvePort;
14
18
  private createBackoffFunction;
15
19
  private sleep;
16
20
  private retryWithBackoff;
@@ -37,4 +37,13 @@ export interface MetadataServiceOptions {
37
37
  * if the function returns a number, the number will be used as seconds duration to wait before the following retry attempt.
38
38
  */
39
39
  backoff?: number | ((numFailures: number) => Promise<void> | number);
40
+ /**
41
+ * the TTL of the token in seconds, defaulting to 21,600 seconds (6 hours)
42
+ */
43
+ tokenTtl?: number;
44
+ /**
45
+ * the port for the endpoint. If not specified, uses the default port for the protocol (80 for HTTP, 443 for HTTPS).
46
+ * can also be provided as a part of the endpoint URL, though an explicit config value will take precedence.
47
+ */
48
+ port?: number;
40
49
  }
@@ -4,7 +4,11 @@ export declare class MetadataService {
4
4
  private config;
5
5
  private retries;
6
6
  private backoffFn;
7
+ private tokenTtl;
8
+ private port?;
7
9
  constructor(options?: MetadataServiceOptions);
10
+ private validateTokenTtl;
11
+ private resolvePort;
8
12
  private createBackoffFunction;
9
13
  private sleep;
10
14
  private retryWithBackoff;
@@ -8,4 +8,6 @@ export interface MetadataServiceOptions {
8
8
  disableFetchToken?: boolean;
9
9
  retries?: number;
10
10
  backoff?: number | ((numFailures: number) => Promise<void> | number);
11
+ tokenTtl?: number;
12
+ port?: number;
11
13
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@aws-sdk/ec2-metadata-service",
3
- "version": "3.955.0",
3
+ "version": "3.957.0",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:types' 'yarn:build:es' && yarn build:cjs",
6
6
  "build:cjs": "node ../../scripts/compilation/inline ec2-metadata-service",
7
7
  "build:es": "tsc -p tsconfig.es.json",
8
- "build:include:deps": "lerna run --scope $npm_package_name --include-dependencies build",
8
+ "build:include:deps": "yarn g:turbo run build -F=\"$npm_package_name\"",
9
9
  "build:types": "tsc -p tsconfig.types.json",
10
10
  "build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
11
11
  "clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
@@ -24,16 +24,16 @@
24
24
  "module": "./dist-es/index.js",
25
25
  "types": "./dist-types/index.d.ts",
26
26
  "dependencies": {
27
- "@aws-sdk/types": "3.953.0",
28
- "@smithy/node-config-provider": "^4.3.6",
29
- "@smithy/node-http-handler": "^4.4.6",
30
- "@smithy/protocol-http": "^5.3.6",
31
- "@smithy/types": "^4.10.0",
32
- "@smithy/util-stream": "^4.5.7",
27
+ "@aws-sdk/types": "3.957.0",
28
+ "@smithy/node-config-provider": "^4.3.7",
29
+ "@smithy/node-http-handler": "^4.4.7",
30
+ "@smithy/protocol-http": "^5.3.7",
31
+ "@smithy/types": "^4.11.0",
32
+ "@smithy/util-stream": "^4.5.8",
33
33
  "tslib": "^2.6.2"
34
34
  },
35
35
  "devDependencies": {
36
- "@aws-sdk/credential-providers": "3.955.0",
36
+ "@aws-sdk/credential-providers": "3.957.0",
37
37
  "@tsconfig/recommended": "1.0.1",
38
38
  "@types/node": "^18.19.69",
39
39
  "concurrently": "7.0.0",