@aws-sdk/ec2-metadata-service 3.899.0 → 3.906.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.
Files changed (2) hide show
  1. package/dist-cjs/index.js +124 -148
  2. package/package.json +8 -8
package/dist-cjs/index.js CHANGED
@@ -1,159 +1,135 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
20
2
 
21
- // src/index.ts
22
- var index_exports = {};
23
- __export(index_exports, {
24
- MetadataService: () => MetadataService
25
- });
26
- module.exports = __toCommonJS(index_exports);
3
+ var nodeConfigProvider = require('@smithy/node-config-provider');
4
+ var nodeHttpHandler = require('@smithy/node-http-handler');
5
+ var protocolHttp = require('@smithy/protocol-http');
6
+ var utilStream = require('@smithy/util-stream');
27
7
 
28
- // src/MetadataService.ts
29
- var import_node_config_provider = require("@smithy/node-config-provider");
30
- var import_node_http_handler = require("@smithy/node-http-handler");
31
- var import_protocol_http = require("@smithy/protocol-http");
32
- var import_util_stream = require("@smithy/util-stream");
8
+ var Endpoint;
9
+ (function (Endpoint) {
10
+ Endpoint["IPv4"] = "http://169.254.169.254";
11
+ Endpoint["IPv6"] = "http://[fd00:ec2::254]";
12
+ })(Endpoint || (Endpoint = {}));
33
13
 
34
- // src/ConfigLoaders.ts
35
- var ENV_ENDPOINT_NAME = "AWS_EC2_METADATA_SERVICE_ENDPOINT";
36
- var CONFIG_ENDPOINT_NAME = "ec2_metadata_service_endpoint";
37
- var ENDPOINT_SELECTORS = {
38
- environmentVariableSelector: /* @__PURE__ */ __name((env) => env[ENV_ENDPOINT_NAME], "environmentVariableSelector"),
39
- configFileSelector: /* @__PURE__ */ __name((profile) => profile[CONFIG_ENDPOINT_NAME], "configFileSelector"),
40
- default: "http://169.254.169.254" /* IPv4 */
41
- };
42
- var ENV_ENDPOINT_MODE_NAME = "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE";
43
- var CONFIG_ENDPOINT_MODE_NAME = "ec2_metadata_service_endpoint_mode";
44
- var ENDPOINT_MODE_SELECTORS = {
45
- environmentVariableSelector: /* @__PURE__ */ __name((env) => env[ENV_ENDPOINT_MODE_NAME], "environmentVariableSelector"),
46
- configFileSelector: /* @__PURE__ */ __name((profile) => profile[CONFIG_ENDPOINT_MODE_NAME], "configFileSelector"),
47
- default: "IPv4" /* IPv4 */
14
+ var EndpointMode;
15
+ (function (EndpointMode) {
16
+ EndpointMode["IPv4"] = "IPv4";
17
+ EndpointMode["IPv6"] = "IPv6";
18
+ })(EndpointMode || (EndpointMode = {}));
19
+
20
+ const ENV_ENDPOINT_NAME = "AWS_EC2_METADATA_SERVICE_ENDPOINT";
21
+ const CONFIG_ENDPOINT_NAME = "ec2_metadata_service_endpoint";
22
+ const ENDPOINT_SELECTORS = {
23
+ environmentVariableSelector: (env) => env[ENV_ENDPOINT_NAME],
24
+ configFileSelector: (profile) => profile[CONFIG_ENDPOINT_NAME],
25
+ default: Endpoint.IPv4,
48
26
  };
49
- var AWS_EC2_METADATA_V1_DISABLED = "AWS_EC2_METADATA_V1_DISABLED";
50
- var PROFILE_AWS_EC2_METADATA_V1_DISABLED = "ec2_metadata_v1_disabled";
51
- var IMDSv1_DISABLED_SELECTORS = {
52
- environmentVariableSelector: /* @__PURE__ */ __name((env) => env[AWS_EC2_METADATA_V1_DISABLED] ? env[AWS_EC2_METADATA_V1_DISABLED] !== "false" : void 0, "environmentVariableSelector"),
53
- configFileSelector: /* @__PURE__ */ __name((profile) => profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED] ? profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED] !== "false" : void 0, "configFileSelector"),
54
- default: false
27
+ ({
28
+ default: EndpointMode.IPv4,
29
+ });
30
+ const AWS_EC2_METADATA_V1_DISABLED = "AWS_EC2_METADATA_V1_DISABLED";
31
+ const PROFILE_AWS_EC2_METADATA_V1_DISABLED = "ec2_metadata_v1_disabled";
32
+ const IMDSv1_DISABLED_SELECTORS = {
33
+ environmentVariableSelector: (env) => env[AWS_EC2_METADATA_V1_DISABLED] ? env[AWS_EC2_METADATA_V1_DISABLED] !== "false" : undefined,
34
+ configFileSelector: (profile) => profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED]
35
+ ? profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED] !== "false"
36
+ : undefined,
37
+ default: false,
55
38
  };
56
39
 
57
- // src/MetadataService.ts
58
- var MetadataService = class {
59
- static {
60
- __name(this, "MetadataService");
61
- }
62
- disableFetchToken;
63
- config;
64
- /**
65
- * Creates a new MetadataService object with a given set of options.
66
- */
67
- constructor(options = {}) {
68
- this.config = (async () => {
69
- const profile = options?.profile || process.env.AWS_PROFILE;
70
- return {
71
- endpoint: options.endpoint ?? await (0, import_node_config_provider.loadConfig)(ENDPOINT_SELECTORS, { profile })(),
72
- httpOptions: {
73
- timeout: options?.httpOptions?.timeout || 0
74
- },
75
- ec2MetadataV1Disabled: options?.ec2MetadataV1Disabled ?? await (0, import_node_config_provider.loadConfig)(IMDSv1_DISABLED_SELECTORS, { profile })()
76
- };
77
- })();
78
- this.disableFetchToken = options?.disableFetchToken || false;
79
- }
80
- async request(path, options) {
81
- const { endpoint, ec2MetadataV1Disabled, httpOptions } = await this.config;
82
- const handler = new import_node_http_handler.NodeHttpHandler({
83
- requestTimeout: httpOptions?.timeout,
84
- connectionTimeout: httpOptions?.timeout
85
- });
86
- const endpointUrl = new URL(endpoint);
87
- const headers = options.headers || {};
88
- if (this.disableFetchToken && ec2MetadataV1Disabled) {
89
- throw new Error("IMDSv1 is disabled and fetching token is disabled, cannot make the request.");
40
+ class MetadataService {
41
+ disableFetchToken;
42
+ config;
43
+ constructor(options = {}) {
44
+ this.config = (async () => {
45
+ const profile = options?.profile || process.env.AWS_PROFILE;
46
+ return {
47
+ endpoint: options.endpoint ?? (await nodeConfigProvider.loadConfig(ENDPOINT_SELECTORS, { profile })()),
48
+ httpOptions: {
49
+ timeout: options?.httpOptions?.timeout || 0,
50
+ },
51
+ ec2MetadataV1Disabled: options?.ec2MetadataV1Disabled ?? (await nodeConfigProvider.loadConfig(IMDSv1_DISABLED_SELECTORS, { profile })()),
52
+ };
53
+ })();
54
+ this.disableFetchToken = options?.disableFetchToken || false;
90
55
  }
91
- if (!this.disableFetchToken) {
92
- try {
93
- headers["x-aws-ec2-metadata-token"] = await this.fetchMetadataToken();
94
- } catch (err) {
95
- if (ec2MetadataV1Disabled) {
96
- throw err;
56
+ async request(path, options) {
57
+ const { endpoint, ec2MetadataV1Disabled, httpOptions } = await this.config;
58
+ const handler = new nodeHttpHandler.NodeHttpHandler({
59
+ requestTimeout: httpOptions?.timeout,
60
+ connectionTimeout: httpOptions?.timeout,
61
+ });
62
+ const endpointUrl = new URL(endpoint);
63
+ const headers = options.headers || {};
64
+ if (this.disableFetchToken && ec2MetadataV1Disabled) {
65
+ throw new Error("IMDSv1 is disabled and fetching token is disabled, cannot make the request.");
66
+ }
67
+ if (!this.disableFetchToken) {
68
+ try {
69
+ headers["x-aws-ec2-metadata-token"] = await this.fetchMetadataToken();
70
+ }
71
+ catch (err) {
72
+ if (ec2MetadataV1Disabled) {
73
+ throw err;
74
+ }
75
+ }
76
+ }
77
+ const request = new protocolHttp.HttpRequest({
78
+ method: options.method || "GET",
79
+ headers: headers,
80
+ hostname: endpointUrl.hostname,
81
+ path: endpointUrl.pathname + path,
82
+ protocol: endpointUrl.protocol,
83
+ });
84
+ try {
85
+ const { response } = await handler.handle(request, {});
86
+ if (response.statusCode === 200 && response.body) {
87
+ return utilStream.sdkStreamMixin(response.body).transformToString();
88
+ }
89
+ else {
90
+ throw new Error(`Request failed with status code ${response.statusCode}`);
91
+ }
92
+ }
93
+ catch (error) {
94
+ throw new Error(`Error making request to the metadata service: ${error}`);
97
95
  }
98
- }
99
- }
100
- const request = new import_protocol_http.HttpRequest({
101
- method: options.method || "GET",
102
- // Default to GET if no method is specified
103
- headers,
104
- hostname: endpointUrl.hostname,
105
- path: endpointUrl.pathname + path,
106
- protocol: endpointUrl.protocol
107
- });
108
- try {
109
- const { response } = await handler.handle(request, {});
110
- if (response.statusCode === 200 && response.body) {
111
- return (0, import_util_stream.sdkStreamMixin)(response.body).transformToString();
112
- } else {
113
- throw new Error(`Request failed with status code ${response.statusCode}`);
114
- }
115
- } catch (error) {
116
- throw new Error(`Error making request to the metadata service: ${error}`);
117
96
  }
118
- }
119
- async fetchMetadataToken() {
120
- const { endpoint, httpOptions } = await this.config;
121
- const handler = new import_node_http_handler.NodeHttpHandler({
122
- requestTimeout: httpOptions?.timeout,
123
- connectionTimeout: httpOptions?.timeout
124
- });
125
- const endpointUrl = new URL(endpoint);
126
- const tokenRequest = new import_protocol_http.HttpRequest({
127
- method: "PUT",
128
- headers: {
129
- "x-aws-ec2-metadata-token-ttl-seconds": "21600"
130
- // 6 hours;
131
- },
132
- hostname: endpointUrl.hostname,
133
- path: "/latest/api/token",
134
- protocol: endpointUrl.protocol
135
- });
136
- try {
137
- const { response } = await handler.handle(tokenRequest, {});
138
- if (response.statusCode === 200 && response.body) {
139
- return (0, import_util_stream.sdkStreamMixin)(response.body).transformToString();
140
- } else {
141
- throw new Error(`Failed to fetch metadata token with status code ${response.statusCode}`);
142
- }
143
- } catch (error) {
144
- if (error?.statusCode === 400) {
145
- throw new Error(`Error fetching metadata token: ${error}`);
146
- } else if (error.message === "TimeoutError" || [403, 404, 405].includes(error.statusCode)) {
147
- this.disableFetchToken = true;
148
- throw new Error(`Error fetching metadata token: ${error}. disableFetchToken is enabled`);
149
- }
150
- throw new Error(`Error fetching metadata token: ${error}`);
97
+ async fetchMetadataToken() {
98
+ const { endpoint, httpOptions } = await this.config;
99
+ const handler = new nodeHttpHandler.NodeHttpHandler({
100
+ requestTimeout: httpOptions?.timeout,
101
+ connectionTimeout: httpOptions?.timeout,
102
+ });
103
+ const endpointUrl = new URL(endpoint);
104
+ const tokenRequest = new protocolHttp.HttpRequest({
105
+ method: "PUT",
106
+ headers: {
107
+ "x-aws-ec2-metadata-token-ttl-seconds": "21600",
108
+ },
109
+ hostname: endpointUrl.hostname,
110
+ path: "/latest/api/token",
111
+ protocol: endpointUrl.protocol,
112
+ });
113
+ try {
114
+ const { response } = await handler.handle(tokenRequest, {});
115
+ if (response.statusCode === 200 && response.body) {
116
+ return utilStream.sdkStreamMixin(response.body).transformToString();
117
+ }
118
+ else {
119
+ throw new Error(`Failed to fetch metadata token with status code ${response.statusCode}`);
120
+ }
121
+ }
122
+ catch (error) {
123
+ if (error?.statusCode === 400) {
124
+ throw new Error(`Error fetching metadata token: ${error}`);
125
+ }
126
+ else if (error.message === "TimeoutError" || [403, 404, 405].includes(error.statusCode)) {
127
+ this.disableFetchToken = true;
128
+ throw new Error(`Error fetching metadata token: ${error}. disableFetchToken is enabled`);
129
+ }
130
+ throw new Error(`Error fetching metadata token: ${error}`);
131
+ }
151
132
  }
152
- }
153
- };
154
- // Annotate the CommonJS export names for ESM import in node:
155
-
156
- 0 && (module.exports = {
157
- MetadataService
158
- });
133
+ }
159
134
 
135
+ exports.MetadataService = MetadataService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/ec2-metadata-service",
3
- "version": "3.899.0",
3
+ "version": "3.906.0",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
6
6
  "build:cjs": "node ../../scripts/compilation/inline ec2-metadata-service",
@@ -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.893.0",
28
- "@smithy/node-config-provider": "^4.2.2",
29
- "@smithy/node-http-handler": "^4.2.1",
30
- "@smithy/protocol-http": "^5.2.1",
31
- "@smithy/types": "^4.5.0",
32
- "@smithy/util-stream": "^4.3.2",
27
+ "@aws-sdk/types": "3.901.0",
28
+ "@smithy/node-config-provider": "^4.3.0",
29
+ "@smithy/node-http-handler": "^4.3.0",
30
+ "@smithy/protocol-http": "^5.3.0",
31
+ "@smithy/types": "^4.6.0",
32
+ "@smithy/util-stream": "^4.4.0",
33
33
  "tslib": "^2.6.2"
34
34
  },
35
35
  "devDependencies": {
36
- "@aws-sdk/credential-providers": "3.899.0",
36
+ "@aws-sdk/credential-providers": "3.906.0",
37
37
  "@tsconfig/recommended": "1.0.1",
38
38
  "@types/node": "^18.19.69",
39
39
  "concurrently": "7.0.0",