@smithy/core 3.27.0 → 3.28.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.
@@ -127,8 +127,11 @@ class Crc32Js {
127
127
  this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
128
128
  }
129
129
  }
130
+ digestSync() {
131
+ return (this.checksum ^ ONES) >>> 0;
132
+ }
130
133
  async digest() {
131
- const value = (this.checksum ^ ONES) >>> 0;
134
+ const value = this.digestSync();
132
135
  const out = new Uint8Array(4);
133
136
  new DataView(out.buffer).setUint32(0, value, false);
134
137
  return out;
@@ -217,8 +217,11 @@ class Crc32Js {
217
217
  this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
218
218
  }
219
219
  }
220
+ digestSync() {
221
+ return (this.checksum ^ ONES) >>> 0;
222
+ }
220
223
  async digest() {
221
- const value = (this.checksum ^ ONES) >>> 0;
224
+ const value = this.digestSync();
222
225
  const out = new Uint8Array(4);
223
226
  new DataView(out.buffer).setUint32(0, value, false);
224
227
  return out;
@@ -237,9 +240,12 @@ function buildNativeClass$1(nativeCrc32) {
237
240
  update(data) {
238
241
  this.value = nativeCrc32(data, this.value);
239
242
  }
243
+ digestSync() {
244
+ return this.value >>> 0;
245
+ }
240
246
  async digest() {
241
247
  const out = new Uint8Array(4);
242
- new DataView(out.buffer).setUint32(0, this.value >>> 0, false);
248
+ new DataView(out.buffer).setUint32(0, this.digestSync(), false);
243
249
  return out;
244
250
  }
245
251
  reset() {
@@ -127,8 +127,11 @@ class Crc32Js {
127
127
  this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
128
128
  }
129
129
  }
130
+ digestSync() {
131
+ return (this.checksum ^ ONES) >>> 0;
132
+ }
130
133
  async digest() {
131
- const value = (this.checksum ^ ONES) >>> 0;
134
+ const value = this.digestSync();
132
135
  const out = new Uint8Array(4);
133
136
  new DataView(out.buffer).setUint32(0, value, false);
134
137
  return out;
@@ -470,12 +470,118 @@ const resolveEndpointsConfig = (input) => {
470
470
  });
471
471
  };
472
472
 
473
+ const AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV";
474
+ const AWS_REGION_ENV = "AWS_REGION";
475
+ const AWS_DEFAULT_REGION_ENV = "AWS_DEFAULT_REGION";
476
+ const ENV_IMDS_DISABLED = "AWS_EC2_METADATA_DISABLED";
477
+ const DEFAULTS_MODE_OPTIONS = ["in-region", "cross-region", "mobile", "standard", "legacy"];
478
+ const IMDS_REGION_PATH = "/latest/meta-data/placement/region";
479
+ const IMDS_TOKEN_PATH = "/latest/api/token";
480
+ const X_AWS_EC2_METADATA_TOKEN = "x-aws-ec2-metadata-token";
481
+ const X_AWS_EC2_METADATA_TOKEN_TTL = "x-aws-ec2-metadata-token-ttl-seconds";
482
+
483
+ const TIMEOUT_MS = 1000;
484
+ const NEG_CACHE_TTL_MS = 60_000;
485
+ let negativeCacheUntil = 0;
486
+ const getInstanceMetadataRegion = async () => {
487
+ if (process.env[ENV_IMDS_DISABLED]) {
488
+ return undefined;
489
+ }
490
+ if (Date.now() < negativeCacheUntil) {
491
+ return undefined;
492
+ }
493
+ try {
494
+ const endpoint = resolveImdsEndpoint();
495
+ const token = (await imdsRequest({
496
+ ...endpoint,
497
+ path: IMDS_TOKEN_PATH,
498
+ method: "PUT",
499
+ headers: {
500
+ [X_AWS_EC2_METADATA_TOKEN_TTL]: "21600",
501
+ },
502
+ })).toString();
503
+ const region = (await imdsRequest({
504
+ ...endpoint,
505
+ path: IMDS_REGION_PATH,
506
+ method: "GET",
507
+ headers: {
508
+ [X_AWS_EC2_METADATA_TOKEN]: token,
509
+ },
510
+ }))
511
+ .toString()
512
+ .trim();
513
+ return region || cacheNegativeAndReturnUndefined();
514
+ }
515
+ catch {
516
+ return cacheNegativeAndReturnUndefined();
517
+ }
518
+ };
519
+ const cacheNegativeAndReturnUndefined = () => {
520
+ negativeCacheUntil = Date.now() + NEG_CACHE_TTL_MS;
521
+ return undefined;
522
+ };
523
+ const resolveImdsEndpoint = () => {
524
+ const envEndpoint = process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT;
525
+ if (envEndpoint) {
526
+ const url = new URL(envEndpoint);
527
+ return {
528
+ hostname: url.hostname.replace(/^\[(.+)]$/, "$1"),
529
+ port: url.port ? Number(url.port) : undefined,
530
+ };
531
+ }
532
+ if (process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE === "IPv6") {
533
+ return { hostname: "fd00:ec2::254" };
534
+ }
535
+ return { hostname: "169.254.169.254" };
536
+ };
537
+ const imdsRequest = async (options) => {
538
+ const { request } = require('node:http');
539
+ return new Promise((resolve, reject) => {
540
+ const req = request({
541
+ hostname: options.hostname,
542
+ port: options.port,
543
+ path: options.path,
544
+ method: options.method,
545
+ headers: options.headers,
546
+ timeout: TIMEOUT_MS,
547
+ signal: AbortSignal.timeout(TIMEOUT_MS),
548
+ });
549
+ req.on("error", (err) => {
550
+ reject(err);
551
+ req.destroy();
552
+ });
553
+ req.on("timeout", () => {
554
+ reject(new Error("TimeoutError from instance metadata service"));
555
+ req.destroy();
556
+ });
557
+ req.on("response", (res) => {
558
+ const { statusCode = 400 } = res;
559
+ if (statusCode < 200 || statusCode >= 300) {
560
+ reject(Object.assign(new Error("Error response received from instance metadata service"), { statusCode }));
561
+ req.destroy();
562
+ return;
563
+ }
564
+ const chunks = [];
565
+ res.on("data", (chunk) => chunks.push(chunk));
566
+ res.on("end", () => {
567
+ resolve(Buffer.concat(chunks));
568
+ req.destroy();
569
+ });
570
+ });
571
+ req.end();
572
+ });
573
+ };
574
+
473
575
  const REGION_ENV_NAME = "AWS_REGION";
474
576
  const REGION_INI_NAME = "region";
475
577
  const NODE_REGION_CONFIG_OPTIONS = {
476
578
  environmentVariableSelector: (env) => env[REGION_ENV_NAME],
477
579
  configFileSelector: (profile) => profile[REGION_INI_NAME],
478
- default: () => {
580
+ default: async () => {
581
+ const region = await getInstanceMetadataRegion();
582
+ if (region) {
583
+ return region;
584
+ }
479
585
  throw new Error("Region is missing");
480
586
  },
481
587
  };
@@ -577,13 +683,6 @@ const getRegionInfo = (region, { useFipsEndpoint = false, useDualstackEndpoint =
577
683
  };
578
684
  };
579
685
 
580
- const AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV";
581
- const AWS_REGION_ENV = "AWS_REGION";
582
- const AWS_DEFAULT_REGION_ENV = "AWS_DEFAULT_REGION";
583
- const ENV_IMDS_DISABLED = "AWS_EC2_METADATA_DISABLED";
584
- const DEFAULTS_MODE_OPTIONS = ["in-region", "cross-region", "mobile", "standard", "legacy"];
585
- const IMDS_REGION_PATH = "/latest/meta-data/placement/region";
586
-
587
686
  const AWS_DEFAULTS_MODE_ENV = "AWS_DEFAULTS_MODE";
588
687
  const AWS_DEFAULTS_MODE_CONFIG = "defaults_mode";
589
688
  const NODE_DEFAULTS_MODE_CONFIG_OPTIONS = {
@@ -633,61 +732,7 @@ const inferPhysicalRegion = async () => {
633
732
  if (process.env[AWS_EXECUTION_ENV] && (process.env[AWS_REGION_ENV] || process.env[AWS_DEFAULT_REGION_ENV])) {
634
733
  return process.env[AWS_REGION_ENV] ?? process.env[AWS_DEFAULT_REGION_ENV];
635
734
  }
636
- if (!process.env[ENV_IMDS_DISABLED]) {
637
- try {
638
- const endpoint = await getImdsEndpoint();
639
- return (await imdsHttpGet({ hostname: endpoint.hostname, path: IMDS_REGION_PATH })).toString();
640
- }
641
- catch (e) {
642
- }
643
- }
644
- };
645
- const getImdsEndpoint = async () => {
646
- const envEndpoint = process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT;
647
- if (envEndpoint) {
648
- const url = new URL(envEndpoint);
649
- return { hostname: url.hostname, path: url.pathname };
650
- }
651
- const envMode = process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE;
652
- if (envMode === "IPv6") {
653
- return { hostname: "fd00:ec2::254", path: "/" };
654
- }
655
- return { hostname: "169.254.169.254", path: "/" };
656
- };
657
- const imdsHttpGet = async ({ hostname, path }) => {
658
- const { request } = require('node:http');
659
- return new Promise((resolve, reject) => {
660
- const req = request({
661
- method: "GET",
662
- hostname: hostname.replace(/^\[(.+)]$/, "$1"),
663
- path,
664
- timeout: 1000,
665
- signal: AbortSignal.timeout(1000),
666
- });
667
- req.on("error", (err) => {
668
- reject(err);
669
- req.destroy();
670
- });
671
- req.on("timeout", () => {
672
- reject(new Error("TimeoutError from instance metadata service"));
673
- req.destroy();
674
- });
675
- req.on("response", (res) => {
676
- const { statusCode = 400 } = res;
677
- if (statusCode < 200 || 300 <= statusCode) {
678
- reject(Object.assign(new Error("Error response received from instance metadata service"), { statusCode }));
679
- req.destroy();
680
- return;
681
- }
682
- const chunks = [];
683
- res.on("data", (chunk) => chunks.push(chunk));
684
- res.on("end", () => {
685
- resolve(Buffer.concat(chunks));
686
- req.destroy();
687
- });
688
- });
689
- req.end();
690
- });
735
+ return getInstanceMetadataRegion();
691
736
  };
692
737
 
693
738
  exports.CONFIG_PREFIX_SEPARATOR = CONFIG_PREFIX_SEPARATOR;
@@ -1,4 +1,4 @@
1
- const { Crc32 } = require("@aws-crypto/crc32");
1
+ const { Crc32 } = require("@smithy/core/checksum");
2
2
  const { toHex, fromHex, toUtf8, fromUtf8 } = require("@smithy/core/serde");
3
3
 
4
4
  class Int64 {
@@ -244,13 +244,14 @@ function splitMessage({ byteLength, byteOffset, buffer }) {
244
244
  const headerLength = view.getUint32(PRELUDE_MEMBER_LENGTH, false);
245
245
  const expectedPreludeChecksum = view.getUint32(PRELUDE_LENGTH, false);
246
246
  const expectedMessageChecksum = view.getUint32(byteLength - CHECKSUM_LENGTH, false);
247
- const checksummer = new Crc32().update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
248
- if (expectedPreludeChecksum !== checksummer.digest()) {
249
- throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digest()})`);
247
+ const checksummer = new Crc32();
248
+ checksummer.update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
249
+ if (expectedPreludeChecksum !== checksummer.digestSync()) {
250
+ throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digestSync()})`);
250
251
  }
251
252
  checksummer.update(new Uint8Array(buffer, byteOffset + PRELUDE_LENGTH, byteLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH)));
252
- if (expectedMessageChecksum !== checksummer.digest()) {
253
- throw new Error(`The message checksum (${checksummer.digest()}) did not match the expected value of ${expectedMessageChecksum}`);
253
+ if (expectedMessageChecksum !== checksummer.digestSync()) {
254
+ throw new Error(`The message checksum (${checksummer.digestSync()}) did not match the expected value of ${expectedMessageChecksum}`);
254
255
  }
255
256
  return {
256
257
  headers: new DataView(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH, headerLength),
@@ -306,10 +307,12 @@ class EventStreamCodec {
306
307
  const checksum = new Crc32();
307
308
  view.setUint32(0, length, false);
308
309
  view.setUint32(4, headers.byteLength, false);
309
- view.setUint32(8, checksum.update(out.subarray(0, 8)).digest(), false);
310
+ checksum.update(out.subarray(0, 8));
311
+ view.setUint32(8, checksum.digestSync(), false);
310
312
  out.set(headers, 12);
311
313
  out.set(body, headers.byteLength + 12);
312
- view.setUint32(length - 4, checksum.update(out.subarray(8, length - 4)).digest(), false);
314
+ checksum.update(out.subarray(8, length - 4));
315
+ view.setUint32(length - 4, checksum.digestSync(), false);
313
316
  return out;
314
317
  }
315
318
  decode(message) {
@@ -1,4 +1,4 @@
1
- const { Crc32 } = require("@aws-crypto/crc32");
1
+ const { Crc32 } = require("@smithy/core/checksum");
2
2
  const { toHex, fromHex, toUtf8, fromUtf8 } = require("@smithy/core/serde");
3
3
  const { Readable } = require("node:stream");
4
4
 
@@ -245,13 +245,14 @@ function splitMessage({ byteLength, byteOffset, buffer }) {
245
245
  const headerLength = view.getUint32(PRELUDE_MEMBER_LENGTH, false);
246
246
  const expectedPreludeChecksum = view.getUint32(PRELUDE_LENGTH, false);
247
247
  const expectedMessageChecksum = view.getUint32(byteLength - CHECKSUM_LENGTH, false);
248
- const checksummer = new Crc32().update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
249
- if (expectedPreludeChecksum !== checksummer.digest()) {
250
- throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digest()})`);
248
+ const checksummer = new Crc32();
249
+ checksummer.update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
250
+ if (expectedPreludeChecksum !== checksummer.digestSync()) {
251
+ throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digestSync()})`);
251
252
  }
252
253
  checksummer.update(new Uint8Array(buffer, byteOffset + PRELUDE_LENGTH, byteLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH)));
253
- if (expectedMessageChecksum !== checksummer.digest()) {
254
- throw new Error(`The message checksum (${checksummer.digest()}) did not match the expected value of ${expectedMessageChecksum}`);
254
+ if (expectedMessageChecksum !== checksummer.digestSync()) {
255
+ throw new Error(`The message checksum (${checksummer.digestSync()}) did not match the expected value of ${expectedMessageChecksum}`);
255
256
  }
256
257
  return {
257
258
  headers: new DataView(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH, headerLength),
@@ -307,10 +308,12 @@ class EventStreamCodec {
307
308
  const checksum = new Crc32();
308
309
  view.setUint32(0, length, false);
309
310
  view.setUint32(4, headers.byteLength, false);
310
- view.setUint32(8, checksum.update(out.subarray(0, 8)).digest(), false);
311
+ checksum.update(out.subarray(0, 8));
312
+ view.setUint32(8, checksum.digestSync(), false);
311
313
  out.set(headers, 12);
312
314
  out.set(body, headers.byteLength + 12);
313
- view.setUint32(length - 4, checksum.update(out.subarray(8, length - 4)).digest(), false);
315
+ checksum.update(out.subarray(8, length - 4));
316
+ view.setUint32(length - 4, checksum.digestSync(), false);
314
317
  return out;
315
318
  }
316
319
  decode(message) {
@@ -15,8 +15,11 @@ export class Crc32Js {
15
15
  this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
16
16
  }
17
17
  }
18
+ digestSync() {
19
+ return (this.checksum ^ ONES) >>> 0;
20
+ }
18
21
  async digest() {
19
- const value = (this.checksum ^ ONES) >>> 0;
22
+ const value = this.digestSync();
20
23
  const out = new Uint8Array(4);
21
24
  new DataView(out.buffer).setUint32(0, value, false);
22
25
  return out;
@@ -9,9 +9,12 @@ function buildNativeClass(nativeCrc32) {
9
9
  update(data) {
10
10
  this.value = nativeCrc32(data, this.value);
11
11
  }
12
+ digestSync() {
13
+ return this.value >>> 0;
14
+ }
12
15
  async digest() {
13
16
  const out = new Uint8Array(4);
14
- new DataView(out.buffer).setUint32(0, this.value >>> 0, false);
17
+ new DataView(out.buffer).setUint32(0, this.digestSync(), false);
15
18
  return out;
16
19
  }
17
20
  reset() {
@@ -1,9 +1,14 @@
1
+ import { getInstanceMetadataRegion } from "./getInstanceMetadataRegion";
1
2
  export const REGION_ENV_NAME = "AWS_REGION";
2
3
  export const REGION_INI_NAME = "region";
3
4
  export const NODE_REGION_CONFIG_OPTIONS = {
4
5
  environmentVariableSelector: (env) => env[REGION_ENV_NAME],
5
6
  configFileSelector: (profile) => profile[REGION_INI_NAME],
6
- default: () => {
7
+ default: async () => {
8
+ const region = await getInstanceMetadataRegion();
9
+ if (region) {
10
+ return region;
11
+ }
7
12
  throw new Error("Region is missing");
8
13
  },
9
14
  };
@@ -0,0 +1,92 @@
1
+ import { ENV_IMDS_DISABLED, IMDS_REGION_PATH, IMDS_TOKEN_PATH, X_AWS_EC2_METADATA_TOKEN, X_AWS_EC2_METADATA_TOKEN_TTL, } from "../../defaults-mode/constants";
2
+ const TIMEOUT_MS = 1000;
3
+ const NEG_CACHE_TTL_MS = 60_000;
4
+ let negativeCacheUntil = 0;
5
+ export const getInstanceMetadataRegion = async () => {
6
+ if (process.env[ENV_IMDS_DISABLED]) {
7
+ return undefined;
8
+ }
9
+ if (Date.now() < negativeCacheUntil) {
10
+ return undefined;
11
+ }
12
+ try {
13
+ const endpoint = resolveImdsEndpoint();
14
+ const token = (await imdsRequest({
15
+ ...endpoint,
16
+ path: IMDS_TOKEN_PATH,
17
+ method: "PUT",
18
+ headers: {
19
+ [X_AWS_EC2_METADATA_TOKEN_TTL]: "21600",
20
+ },
21
+ })).toString();
22
+ const region = (await imdsRequest({
23
+ ...endpoint,
24
+ path: IMDS_REGION_PATH,
25
+ method: "GET",
26
+ headers: {
27
+ [X_AWS_EC2_METADATA_TOKEN]: token,
28
+ },
29
+ }))
30
+ .toString()
31
+ .trim();
32
+ return region || cacheNegativeAndReturnUndefined();
33
+ }
34
+ catch {
35
+ return cacheNegativeAndReturnUndefined();
36
+ }
37
+ };
38
+ const cacheNegativeAndReturnUndefined = () => {
39
+ negativeCacheUntil = Date.now() + NEG_CACHE_TTL_MS;
40
+ return undefined;
41
+ };
42
+ const resolveImdsEndpoint = () => {
43
+ const envEndpoint = process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT;
44
+ if (envEndpoint) {
45
+ const url = new URL(envEndpoint);
46
+ return {
47
+ hostname: url.hostname.replace(/^\[(.+)]$/, "$1"),
48
+ port: url.port ? Number(url.port) : undefined,
49
+ };
50
+ }
51
+ if (process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE === "IPv6") {
52
+ return { hostname: "fd00:ec2::254" };
53
+ }
54
+ return { hostname: "169.254.169.254" };
55
+ };
56
+ const imdsRequest = async (options) => {
57
+ const { request } = await import("node:http");
58
+ return new Promise((resolve, reject) => {
59
+ const req = request({
60
+ hostname: options.hostname,
61
+ port: options.port,
62
+ path: options.path,
63
+ method: options.method,
64
+ headers: options.headers,
65
+ timeout: TIMEOUT_MS,
66
+ signal: AbortSignal.timeout(TIMEOUT_MS),
67
+ });
68
+ req.on("error", (err) => {
69
+ reject(err);
70
+ req.destroy();
71
+ });
72
+ req.on("timeout", () => {
73
+ reject(new Error("TimeoutError from instance metadata service"));
74
+ req.destroy();
75
+ });
76
+ req.on("response", (res) => {
77
+ const { statusCode = 400 } = res;
78
+ if (statusCode < 200 || statusCode >= 300) {
79
+ reject(Object.assign(new Error("Error response received from instance metadata service"), { statusCode }));
80
+ req.destroy();
81
+ return;
82
+ }
83
+ const chunks = [];
84
+ res.on("data", (chunk) => chunks.push(chunk));
85
+ res.on("end", () => {
86
+ resolve(Buffer.concat(chunks));
87
+ req.destroy();
88
+ });
89
+ });
90
+ req.end();
91
+ });
92
+ };
@@ -4,3 +4,6 @@ export const AWS_DEFAULT_REGION_ENV = "AWS_DEFAULT_REGION";
4
4
  export const ENV_IMDS_DISABLED = "AWS_EC2_METADATA_DISABLED";
5
5
  export const DEFAULTS_MODE_OPTIONS = ["in-region", "cross-region", "mobile", "standard", "legacy"];
6
6
  export const IMDS_REGION_PATH = "/latest/meta-data/placement/region";
7
+ export const IMDS_TOKEN_PATH = "/latest/api/token";
8
+ export const X_AWS_EC2_METADATA_TOKEN = "x-aws-ec2-metadata-token";
9
+ export const X_AWS_EC2_METADATA_TOKEN_TTL = "x-aws-ec2-metadata-token-ttl-seconds";
@@ -1,7 +1,8 @@
1
1
  import { NODE_REGION_CONFIG_OPTIONS } from "../config-resolver/regionConfig/config";
2
+ import { getInstanceMetadataRegion } from "../config-resolver/regionConfig/getInstanceMetadataRegion";
2
3
  import { loadConfig } from "../node-config-provider/configLoader";
3
4
  import { memoize } from "../property-provider/memoize";
4
- import { AWS_DEFAULT_REGION_ENV, AWS_EXECUTION_ENV, AWS_REGION_ENV, DEFAULTS_MODE_OPTIONS, ENV_IMDS_DISABLED, IMDS_REGION_PATH, } from "./constants";
5
+ import { AWS_DEFAULT_REGION_ENV, AWS_EXECUTION_ENV, AWS_REGION_ENV, DEFAULTS_MODE_OPTIONS } from "./constants";
5
6
  import { NODE_DEFAULTS_MODE_CONFIG_OPTIONS } from "./defaultsModeConfig";
6
7
  export const resolveDefaultsModeConfig = ({ region = loadConfig(NODE_REGION_CONFIG_OPTIONS), defaultsMode = loadConfig(NODE_DEFAULTS_MODE_CONFIG_OPTIONS), } = {}) => memoize(async () => {
7
8
  const mode = typeof defaultsMode === "function" ? await defaultsMode() : defaultsMode;
@@ -40,59 +41,5 @@ const inferPhysicalRegion = async () => {
40
41
  if (process.env[AWS_EXECUTION_ENV] && (process.env[AWS_REGION_ENV] || process.env[AWS_DEFAULT_REGION_ENV])) {
41
42
  return process.env[AWS_REGION_ENV] ?? process.env[AWS_DEFAULT_REGION_ENV];
42
43
  }
43
- if (!process.env[ENV_IMDS_DISABLED]) {
44
- try {
45
- const endpoint = await getImdsEndpoint();
46
- return (await imdsHttpGet({ hostname: endpoint.hostname, path: IMDS_REGION_PATH })).toString();
47
- }
48
- catch (e) {
49
- }
50
- }
51
- };
52
- const getImdsEndpoint = async () => {
53
- const envEndpoint = process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT;
54
- if (envEndpoint) {
55
- const url = new URL(envEndpoint);
56
- return { hostname: url.hostname, path: url.pathname };
57
- }
58
- const envMode = process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE;
59
- if (envMode === "IPv6") {
60
- return { hostname: "fd00:ec2::254", path: "/" };
61
- }
62
- return { hostname: "169.254.169.254", path: "/" };
63
- };
64
- const imdsHttpGet = async ({ hostname, path }) => {
65
- const { request } = await import("node:http");
66
- return new Promise((resolve, reject) => {
67
- const req = request({
68
- method: "GET",
69
- hostname: hostname.replace(/^\[(.+)]$/, "$1"),
70
- path,
71
- timeout: 1000,
72
- signal: AbortSignal.timeout(1000),
73
- });
74
- req.on("error", (err) => {
75
- reject(err);
76
- req.destroy();
77
- });
78
- req.on("timeout", () => {
79
- reject(new Error("TimeoutError from instance metadata service"));
80
- req.destroy();
81
- });
82
- req.on("response", (res) => {
83
- const { statusCode = 400 } = res;
84
- if (statusCode < 200 || 300 <= statusCode) {
85
- reject(Object.assign(new Error("Error response received from instance metadata service"), { statusCode }));
86
- req.destroy();
87
- return;
88
- }
89
- const chunks = [];
90
- res.on("data", (chunk) => chunks.push(chunk));
91
- res.on("end", () => {
92
- resolve(Buffer.concat(chunks));
93
- req.destroy();
94
- });
95
- });
96
- req.end();
97
- });
44
+ return getInstanceMetadataRegion();
98
45
  };
@@ -1,4 +1,4 @@
1
- import { Crc32 } from "@aws-crypto/crc32";
1
+ import { Crc32 } from "@smithy/core/checksum";
2
2
  import { HeaderMarshaller } from "./HeaderMarshaller";
3
3
  import { splitMessage } from "./splitMessage";
4
4
  export class EventStreamCodec {
@@ -49,10 +49,12 @@ export class EventStreamCodec {
49
49
  const checksum = new Crc32();
50
50
  view.setUint32(0, length, false);
51
51
  view.setUint32(4, headers.byteLength, false);
52
- view.setUint32(8, checksum.update(out.subarray(0, 8)).digest(), false);
52
+ checksum.update(out.subarray(0, 8));
53
+ view.setUint32(8, checksum.digestSync(), false);
53
54
  out.set(headers, 12);
54
55
  out.set(body, headers.byteLength + 12);
55
- view.setUint32(length - 4, checksum.update(out.subarray(8, length - 4)).digest(), false);
56
+ checksum.update(out.subarray(8, length - 4));
57
+ view.setUint32(length - 4, checksum.digestSync(), false);
56
58
  return out;
57
59
  }
58
60
  decode(message) {
@@ -1,4 +1,4 @@
1
- import { Crc32 } from "@aws-crypto/crc32";
1
+ import { Crc32 } from "@smithy/core/checksum";
2
2
  const PRELUDE_MEMBER_LENGTH = 4;
3
3
  const PRELUDE_LENGTH = PRELUDE_MEMBER_LENGTH * 2;
4
4
  const CHECKSUM_LENGTH = 4;
@@ -15,13 +15,14 @@ export function splitMessage({ byteLength, byteOffset, buffer }) {
15
15
  const headerLength = view.getUint32(PRELUDE_MEMBER_LENGTH, false);
16
16
  const expectedPreludeChecksum = view.getUint32(PRELUDE_LENGTH, false);
17
17
  const expectedMessageChecksum = view.getUint32(byteLength - CHECKSUM_LENGTH, false);
18
- const checksummer = new Crc32().update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
19
- if (expectedPreludeChecksum !== checksummer.digest()) {
20
- throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digest()})`);
18
+ const checksummer = new Crc32();
19
+ checksummer.update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
20
+ if (expectedPreludeChecksum !== checksummer.digestSync()) {
21
+ throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digestSync()})`);
21
22
  }
22
23
  checksummer.update(new Uint8Array(buffer, byteOffset + PRELUDE_LENGTH, byteLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH)));
23
- if (expectedMessageChecksum !== checksummer.digest()) {
24
- throw new Error(`The message checksum (${checksummer.digest()}) did not match the expected value of ${expectedMessageChecksum}`);
24
+ if (expectedMessageChecksum !== checksummer.digestSync()) {
25
+ throw new Error(`The message checksum (${checksummer.digestSync()}) did not match the expected value of ${expectedMessageChecksum}`);
25
26
  }
26
27
  return {
27
28
  headers: new DataView(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH, headerLength),
@@ -8,6 +8,11 @@ export declare class Crc32Js implements Checksum {
8
8
  readonly digestLength = 4;
9
9
  private checksum;
10
10
  update(data: Uint8Array): void;
11
+ /**
12
+ * Used by EventStreamCodec.
13
+ * @internal
14
+ */
15
+ digestSync(): number;
11
16
  digest(): Promise<Uint8Array>;
12
17
  reset(): void;
13
18
  }
@@ -6,6 +6,11 @@ import type { Checksum } from "@smithy/types";
6
6
  */
7
7
  export interface Crc32Node extends Checksum {
8
8
  readonly digestLength: 4;
9
+ /**
10
+ * Used by EventStreamCodec.
11
+ * @internal
12
+ */
13
+ digestSync(): number;
9
14
  }
10
15
  /**
11
16
  * @public
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns the region of the host from the EC2 Instance Metadata Service (IMDSv2),
3
+ * or undefined if unavailable.
4
+ *
5
+ * @internal
6
+ */
7
+ export declare const getInstanceMetadataRegion: () => Promise<string | undefined>;
@@ -22,3 +22,15 @@ export declare const DEFAULTS_MODE_OPTIONS: string[];
22
22
  * @internal
23
23
  */
24
24
  export declare const IMDS_REGION_PATH = "/latest/meta-data/placement/region";
25
+ /**
26
+ * @internal
27
+ */
28
+ export declare const IMDS_TOKEN_PATH = "/latest/api/token";
29
+ /**
30
+ * @internal
31
+ */
32
+ export declare const X_AWS_EC2_METADATA_TOKEN = "x-aws-ec2-metadata-token";
33
+ /**
34
+ * @internal
35
+ */
36
+ export declare const X_AWS_EC2_METADATA_TOKEN_TTL = "x-aws-ec2-metadata-token-ttl-seconds";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smithy/core",
3
- "version": "3.27.0",
3
+ "version": "3.28.0",
4
4
  "scripts": {
5
5
  "benchmark:cbor": "node ./scripts/cbor-perf.mjs",
6
6
  "benchmark:checksum": "node ./scripts/checksum-perf.mjs",
@@ -170,7 +170,6 @@
170
170
  "license": "Apache-2.0",
171
171
  "sideEffects": false,
172
172
  "dependencies": {
173
- "@aws-crypto/crc32": "5.2.0",
174
173
  "@smithy/types": "^4.15.0",
175
174
  "tslib": "^2.6.2"
176
175
  },