@abtnode/util 1.16.25-next-bc2f4c63 → 1.16.25

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.
@@ -1,12 +1,18 @@
1
- const axios = require('axios');
2
1
  const debug = require('debug')('get-ec2-meta');
2
+ const axios = require('./axios');
3
3
 
4
4
  const HOST = 'http://169.254.169.254/latest';
5
5
 
6
6
  // Link: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
7
7
  // Link: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-add-user-data.html
8
8
  const getEc2Meta = async (key, timeout = 5000) => {
9
- const url = key === 'user-data' ? `${HOST}/${key}` : `${HOST}/meta-data/${key}`;
9
+ let url = `${HOST}/meta-data/${key}`;
10
+ if (key === 'user-data') {
11
+ url = `${HOST}/${key}`;
12
+ }
13
+ if (key === 'instance-identity') {
14
+ url = `${HOST}/dynamic/instance-identity/document`;
15
+ }
10
16
 
11
17
  try {
12
18
  const result = await axios.get(url, { timeout });
@@ -17,7 +23,30 @@ const getEc2Meta = async (key, timeout = 5000) => {
17
23
 
18
24
  return '';
19
25
  } catch (err) {
20
- debug('Failed to fetch ec2 meta', err.message);
26
+ debug('Failed to fetch ec2 meta', err);
27
+
28
+ if (err.response?.status === 401) {
29
+ try {
30
+ let result = await axios.put(
31
+ `${HOST}/api/token`,
32
+ {},
33
+ {
34
+ timeout,
35
+ headers: {
36
+ 'X-aws-ec2-metadata-token-ttl-seconds': 21600,
37
+ },
38
+ }
39
+ );
40
+ result = await axios.get(url, { timeout, headers: { 'X-aws-ec2-metadata-token': result.data } });
41
+ if (result.status === 200) {
42
+ return result.data;
43
+ }
44
+ return '';
45
+ } catch (e) {
46
+ debug('Failed to fetch ec2 meta with auth', e);
47
+ }
48
+ }
49
+
21
50
  return '';
22
51
  }
23
52
  };
package/lib/is-ec2.js CHANGED
@@ -1,15 +1,10 @@
1
- const axios = require('./axios');
1
+ const getEc2Meta = require('./get-ec2-meta');
2
2
 
3
3
  // Whether we are running in an pre-baked image
4
4
  // refer: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
5
5
  async function isEC2() {
6
- try {
7
- const url = 'http://169.254.169.254/latest/dynamic/instance-identity/document';
8
- const { status } = await axios.get(url, { timeout: 1000 });
9
- return status === 200;
10
- } catch {
11
- return false;
12
- }
6
+ const meta = await getEc2Meta('instance-identity');
7
+ return !!meta;
13
8
  }
14
9
 
15
10
  module.exports = isEC2;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.25-next-bc2f4c63",
6
+ "version": "1.16.25",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,10 +18,10 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "Apache-2.0",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.16.25-next-bc2f4c63",
22
- "@abtnode/logger": "1.16.25-next-bc2f4c63",
23
- "@blocklet/constant": "1.16.25-next-bc2f4c63",
24
- "@blocklet/meta": "1.16.25-next-bc2f4c63",
21
+ "@abtnode/constant": "1.16.25",
22
+ "@abtnode/logger": "1.16.25",
23
+ "@blocklet/constant": "1.16.25",
24
+ "@blocklet/meta": "1.16.25",
25
25
  "@ocap/client": "1.18.113",
26
26
  "@ocap/mcrypto": "1.18.113",
27
27
  "@ocap/util": "1.18.113",
@@ -76,5 +76,5 @@
76
76
  "fs-extra": "^11.2.0",
77
77
  "jest": "^29.7.0"
78
78
  },
79
- "gitHead": "499ecf2bde0cdb25dbfdd1cf7807212f5fe37ad9"
79
+ "gitHead": "8752b9a0caa8745e16e1de8db4ef696b713ffb4d"
80
80
  }