@abtnode/util 1.6.17 → 1.6.18

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.
@@ -76,17 +76,4 @@ const updateDaemon = async ({ ips, wallet, didRegistryUrl, domain }) => {
76
76
  }
77
77
  };
78
78
 
79
- const updateBlocklet = async ({ nodeDid, wallet, didRegistryUrl, domain }) => {
80
- const records = [
81
- {
82
- type: 'CNAME',
83
- rr: wallet.address.toLowerCase(),
84
- value: `${nodeDid.toLowerCase()}.${domain}`,
85
- domain,
86
- },
87
- ];
88
-
89
- await updateWithRetry({ records, didRegistryUrl, wallet });
90
- };
91
-
92
- module.exports = { updateDaemon, updateBlocklet };
79
+ module.exports = { updateDaemon };
package/lib/get-ip.js CHANGED
@@ -14,23 +14,23 @@ const getExternalIp = async ({ v6 = false, timeout = 5000 } = {}) => {
14
14
  }
15
15
  };
16
16
 
17
- const getIP = async ({ includeV6 = false, timeout = 5000 } = {}) => {
17
+ const getIP = async ({ includeV6 = false, timeout = 5000, includeExternal = true } = {}) => {
18
18
  try {
19
19
  if (isEC2()) {
20
20
  const [internal, external, internalV6, externalV6] = await Promise.all([
21
21
  getEc2Meta('local-ipv4', timeout),
22
- getEc2Meta('public-ipv4', timeout),
22
+ includeExternal ? getEc2Meta('public-ipv4', timeout) : '',
23
23
  includeV6 ? getEc2Meta('local-ipv6', timeout) : '',
24
- includeV6 ? getEc2Meta('public-ipv6', timeout) : '',
24
+ includeV6 && includeExternal ? getEc2Meta('public-ipv6', timeout) : '',
25
25
  ]);
26
26
  return { internal, external, internalV6, externalV6 };
27
27
  }
28
28
 
29
29
  const [internal, external, internalV6, externalV6] = await Promise.all([
30
30
  internalIp.v4(),
31
- getExternalIp({ timeout }),
31
+ includeExternal ? getExternalIp({ timeout }) : '',
32
32
  includeV6 ? internalIp.v6() : '',
33
- includeV6 ? getExternalIp({ v6: true, timeout }) : '',
33
+ includeV6 && includeExternal ? getExternalIp({ v6: true, timeout }) : '',
34
34
  ]);
35
35
  return { internal, external, internalV6, externalV6 };
36
36
  } catch (err) {
@@ -0,0 +1,130 @@
1
+ /* eslint-disable prefer-const */
2
+ /* eslint-disable no-nested-ternary */
3
+ const fs = require('fs');
4
+ const which = require('which');
5
+ const stream = require('stream');
6
+ const spawn = require('cross-spawn');
7
+
8
+ class PrefixTransform extends stream.Transform {
9
+ constructor(prefix, state) {
10
+ super();
11
+
12
+ this.prefix = prefix;
13
+ this.state = state;
14
+ }
15
+
16
+ // transform all output from child process with a prefix for better readability
17
+ _transform(chunk, _encoding, callback) {
18
+ const { prefix, state } = this;
19
+ const nPrefix = `\n${prefix}`;
20
+ const firstPrefix = state.lastIsLineBreak ? prefix : state.lastPrefix !== prefix ? '\n' : '';
21
+ const prefixed = `${firstPrefix}${chunk}`.replace(/\n/g, nPrefix);
22
+ const index = prefixed.indexOf(prefix, Math.max(0, prefixed.length - prefix.length));
23
+
24
+ state.lastPrefix = prefix;
25
+ state.lastIsLineBreak = index !== -1;
26
+
27
+ callback(null, index !== -1 ? prefixed.slice(0, index) : prefixed);
28
+ }
29
+ }
30
+
31
+ const wrapStream = (next, prefix, state) => {
32
+ const transform = new PrefixTransform(`[${prefix}] `, state);
33
+ transform.pipe(next);
34
+ return transform;
35
+ };
36
+
37
+ /**
38
+ * Run script with a childProcess.spawn
39
+ *
40
+ * @param {string} script
41
+ * @param {string} label
42
+ * @param {object} options.env
43
+ * @param {string} options.cwd
44
+ * @param {number} options.timeout
45
+ * @param {bool} options.silent
46
+ * @return {Promise}
47
+ */
48
+ const runScript = (script, label, options = {}) => {
49
+ if (!script) {
50
+ throw new Error('script is required');
51
+ }
52
+ if (!label) {
53
+ throw new Error('label is required');
54
+ }
55
+
56
+ let child = null;
57
+
58
+ const cleanup = () => {
59
+ if (child) {
60
+ child.kill();
61
+ child = null;
62
+ }
63
+ };
64
+
65
+ const promise = new Promise((resolve, reject) => {
66
+ process.stdout.setMaxListeners(0);
67
+ process.stderr.setMaxListeners(0);
68
+
69
+ let [command, ...args] = script.split(' ');
70
+ if (fs.existsSync(command) === false) {
71
+ command = which.sync(command);
72
+ if (!command) {
73
+ reject(new Error(`Command not found: ${command}`));
74
+ return;
75
+ }
76
+ }
77
+
78
+ const state = {
79
+ lastPrefix: null,
80
+ lastIsLineBreak: true,
81
+ };
82
+
83
+ const stdout = wrapStream(process.stdout, label, state);
84
+ const stderr = wrapStream(process.stderr, label, state);
85
+
86
+ child = spawn(command, args, {
87
+ timeout: 30 * 60 * 1000,
88
+ detached: false,
89
+ shell: false,
90
+ stdio: ['ignore', 'pipe', 'pipe'],
91
+ ...options,
92
+ });
93
+
94
+ if (!options.silent) {
95
+ child.stdout.pipe(stdout, { end: false });
96
+ child.stderr.pipe(stderr, { end: false });
97
+ }
98
+
99
+ let hasUnhandledRejection = false;
100
+ const errorMessages = [];
101
+ child.stderr.on('data', (err) => {
102
+ errorMessages.push(err);
103
+ if (err.includes('UnhandledPromiseRejectionWarning')) {
104
+ hasUnhandledRejection = true;
105
+ }
106
+ });
107
+
108
+ child.on('error', (err) => {
109
+ console.error(err);
110
+ errorMessages.push(err.message);
111
+ return reject(new Error(errorMessages.join('\r\n')));
112
+ });
113
+
114
+ child.on('close', (code) => {
115
+ if (errorMessages.length > 0) {
116
+ if (code !== 0 || hasUnhandledRejection) {
117
+ return reject(new Error(errorMessages.join('\r\n')));
118
+ }
119
+ }
120
+
121
+ return resolve({ code, script });
122
+ });
123
+ });
124
+
125
+ promise.abort = cleanup;
126
+
127
+ return promise;
128
+ };
129
+
130
+ module.exports = runScript;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.6.17",
6
+ "version": "1.6.18",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -24,6 +24,7 @@
24
24
  "axios": "^0.21.4",
25
25
  "axios-mock-adapter": "^1.20.0",
26
26
  "axon": "^2.0.3",
27
+ "cross-spawn": "^7.0.3",
27
28
  "debug": "^4.3.3",
28
29
  "find-up": "^5.0.0",
29
30
  "flush-write-stream": "^2.0.0",
@@ -44,7 +45,8 @@
44
45
  "through2-filter": "^3.0.0",
45
46
  "through2-map": "^3.0.0",
46
47
  "to-semver": "^3.0.0",
47
- "url-join": "^4.0.1"
48
+ "url-join": "^4.0.1",
49
+ "which": "^2.0.2"
48
50
  },
49
51
  "devDependencies": {
50
52
  "detect-port": "^1.3.0",
@@ -52,5 +54,5 @@
52
54
  "fs-extra": "^10.0.0",
53
55
  "jest": "^27.4.5"
54
56
  },
55
- "gitHead": "d567a622a779eba43c95eaeb4633cb2b276bb7b9"
57
+ "gitHead": "2f02f166869d8ebedc0466068f6ed90ab3e07b87"
56
58
  }