@abtnode/util 1.8.47 → 1.8.49

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/lib/get-ip.js CHANGED
@@ -16,7 +16,7 @@ const getExternalIp = async ({ v6 = false, timeout = 5000 } = {}) => {
16
16
 
17
17
  const getIP = async ({ includeV6 = false, timeout = 5000, includeExternal = true } = {}) => {
18
18
  try {
19
- if (isEC2()) {
19
+ if (await isEC2()) {
20
20
  const [internal, external, internalV6, externalV6] = await Promise.all([
21
21
  getEc2Meta('local-ipv4', timeout),
22
22
  includeExternal ? getEc2Meta('public-ipv4', timeout) : '',
package/lib/is-ec2.js CHANGED
@@ -1,14 +1,15 @@
1
- const fs = require('fs');
1
+ const axios = require('./axios');
2
2
 
3
3
  // Whether we are running in an pre-baked image
4
- function isEC2() {
5
- const identifyFile = '/sys/hypervisor/uuid';
6
- if (fs.existsSync(identifyFile)) {
7
- const identity = fs.readFileSync(identifyFile).toString();
8
- return identity.toLowerCase().startsWith('ec2');
4
+ // refer: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
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;
9
12
  }
10
-
11
- return false;
12
13
  }
13
14
 
14
15
  module.exports = isEC2;
@@ -0,0 +1,136 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const get = require('lodash/get');
4
+
5
+ const logger = require('@abtnode/logger')('@abtnode/util:logo-middleware');
6
+
7
+ const ensureBlockletExist = async (req, res, next) => {
8
+ const { blocklet } = req;
9
+
10
+ if (!blocklet || !get(blocklet, 'env.appDir')) {
11
+ res.sendFallbackLogo();
12
+ return;
13
+ }
14
+
15
+ next();
16
+ };
17
+
18
+ const ensureCustomSquareLogo = async (req, res, next) => {
19
+ const { blocklet, sendOptions } = req;
20
+
21
+ const customLogoSquare = get(blocklet, 'environmentObj.BLOCKLET_APP_LOGO_SQUARE');
22
+
23
+ if (customLogoSquare) {
24
+ if (customLogoSquare.startsWith('http')) {
25
+ res.redirect(customLogoSquare);
26
+ return;
27
+ }
28
+
29
+ const logoFile = path.join(get(blocklet, 'env.appDir'), customLogoSquare);
30
+ if (fs.existsSync(logoFile)) {
31
+ res.sendFile(logoFile, sendOptions);
32
+ return;
33
+ }
34
+ }
35
+
36
+ next();
37
+ };
38
+
39
+ const ensureBundleLogo = async (req, res, next) => {
40
+ const { blocklet, sendOptions } = req;
41
+
42
+ if (blocklet.meta.logo) {
43
+ const logoFile = path.join(get(blocklet, 'env.appDir'), blocklet.meta.logo);
44
+
45
+ if (fs.existsSync(logoFile)) {
46
+ res.sendFile(logoFile, sendOptions);
47
+ return;
48
+ }
49
+ }
50
+
51
+ next();
52
+ };
53
+
54
+ const fallbackLogo = async (req, res) => {
55
+ res.sendFallbackLogo();
56
+ };
57
+
58
+ // eslint-disable-next-line no-unused-vars
59
+ const cacheError = async (err, req, res, next) => {
60
+ logger.error('failed to send blocklet logo', { url: req.url, error: err });
61
+ res.sendFallbackLogo();
62
+ };
63
+
64
+ const ensureDefaultLogo = async (req, res, next) => {
65
+ const { blocklet, sendOptions } = req;
66
+
67
+ if (blocklet && get(blocklet, 'env.dataDir')) {
68
+ get(blocklet, 'env.dataDir');
69
+
70
+ const logoSvgFile = path.join(get(blocklet, 'env.dataDir'), 'logo.svg');
71
+ if (fs.existsSync(logoSvgFile)) {
72
+ res.sendFile(logoSvgFile, sendOptions);
73
+ return;
74
+ }
75
+
76
+ const logoPngFile = path.join(get(blocklet, 'env.dataDir'), 'logo.png');
77
+ if (fs.existsSync(logoPngFile)) {
78
+ res.sendFile(logoPngFile, sendOptions);
79
+ return;
80
+ }
81
+ }
82
+
83
+ next();
84
+ };
85
+
86
+ const ensureCustomFavicon = async (req, res, next) => {
87
+ const { blocklet, sendOptions } = req;
88
+
89
+ const customLogoFavicon = get(blocklet, 'environmentObj.BLOCKLET_APP_LOGO_FAVICON');
90
+ if (customLogoFavicon) {
91
+ if (customLogoFavicon.startsWith('http')) {
92
+ res.redirect(customLogoFavicon);
93
+ return;
94
+ }
95
+
96
+ const logoFile = path.join(get(blocklet, 'env.appDir'), customLogoFavicon);
97
+ if (fs.existsSync(logoFile)) {
98
+ res.sendFile(logoFile, sendOptions);
99
+ return;
100
+ }
101
+ }
102
+
103
+ next();
104
+ };
105
+
106
+ const attachSendLogoContext =
107
+ ({ onSendFallbackLogo, onGetBlocklet }) =>
108
+ async (req, res, next) => {
109
+ const sendOptions = { maxAge: '1d' };
110
+
111
+ req.sendOptions = sendOptions;
112
+
113
+ try {
114
+ req.blocklet = await onGetBlocklet({ req });
115
+ } catch (error) {
116
+ logger.error('failed to get blocklet', { url: req.url, error });
117
+ req.blocklet = null;
118
+ }
119
+
120
+ res.sendFallbackLogo = () => {
121
+ onSendFallbackLogo({ res, sendOptions });
122
+ };
123
+
124
+ next();
125
+ };
126
+
127
+ module.exports = {
128
+ attachSendLogoContext,
129
+ ensureBlockletExist,
130
+ ensureCustomSquareLogo,
131
+ ensureBundleLogo,
132
+ ensureCustomFavicon,
133
+ ensureDefaultLogo,
134
+ fallbackLogo,
135
+ cacheError,
136
+ };
package/lib/security.js CHANGED
@@ -30,8 +30,16 @@ const formatEnv = (raw, stringifyObject = true) => {
30
30
  return value;
31
31
  };
32
32
 
33
+ // https://github.com/joaquimserafim/base64-url/blob/54d9c9ede66a8724f280cf24fd18c38b9a53915f/index.js#L10
34
+ const escape = (str) => str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
35
+ const encodeEncryptionKey = (key) => escape(Buffer.from(key).toString('base64'));
36
+ const unescape = (str) => (str + '==='.slice((str.length + 3) % 4)).replace(/-/g, '+').replace(/_/g, '/');
37
+ const decodeEncryptionKey = (str) => new Uint8Array(Buffer.from(unescape(str), 'base64'));
38
+
33
39
  module.exports = {
34
40
  encrypt,
35
41
  decrypt,
36
42
  formatEnv,
43
+ encodeEncryptionKey,
44
+ decodeEncryptionKey,
37
45
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.8.47",
6
+ "version": "1.8.49",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,13 +18,13 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.8.47",
22
- "@abtnode/logger": "1.8.47",
23
- "@arcblock/jwt": "^1.18.31",
24
- "@blocklet/constant": "1.8.47",
25
- "@ocap/mcrypto": "1.18.31",
26
- "@ocap/util": "1.18.31",
27
- "@ocap/wallet": "1.18.31",
21
+ "@abtnode/constant": "1.8.49",
22
+ "@abtnode/logger": "1.8.49",
23
+ "@arcblock/jwt": "^1.18.32",
24
+ "@blocklet/constant": "1.8.49",
25
+ "@ocap/mcrypto": "1.18.32",
26
+ "@ocap/util": "1.18.32",
27
+ "@ocap/wallet": "1.18.32",
28
28
  "axios": "^0.27.2",
29
29
  "axios-mock-adapter": "^1.21.2",
30
30
  "axon": "^2.0.3",
@@ -63,5 +63,5 @@
63
63
  "fs-extra": "^10.1.0",
64
64
  "jest": "^27.5.1"
65
65
  },
66
- "gitHead": "87f368c3add9af5ae5c9db3b34682b743032acb4"
66
+ "gitHead": "627ac4fb21957fcbaf7c2c21e77de0f51ef9e30d"
67
67
  }