@abtnode/util 1.16.54-beta-20251024-030947-6f2889bf → 1.16.54-beta-20251027-105624-dfa978f8

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.
@@ -7,6 +7,7 @@ const streamToPromise = require('stream-to-promise');
7
7
  const { version } = require('../package.json');
8
8
  const tryWithTimeout = require('./try-with-timeout');
9
9
  const axios = require('./axios');
10
+ const sleep = require('./sleep');
10
11
 
11
12
  const CANCEL = '__cancel__';
12
13
 
@@ -66,11 +67,13 @@ const downloadFile = async (
66
67
  // eslint-disable-next-line no-promise-executor-return
67
68
  checkCanceled = () => new Promise((resolve) => resolve(false)),
68
69
  onProgress,
70
+ delayTime = 0,
69
71
  } = {},
70
72
  context = {}
71
73
  ) => {
72
74
  const CONNECTION_TIMEOUT = 20 * 1000;
73
75
  const source = CancelToken.source();
76
+ let isCancelled = false;
74
77
 
75
78
  try {
76
79
  return await tryWithTimeout(async () => {
@@ -79,8 +82,8 @@ const downloadFile = async (
79
82
  }, CONNECTION_TIMEOUT);
80
83
 
81
84
  let fileStream;
82
-
83
85
  const cancelDownload = () => {
86
+ isCancelled = true;
84
87
  clearTimeout(timer);
85
88
  source.cancel('Manual cancel');
86
89
  if (fileStream) {
@@ -89,6 +92,14 @@ const downloadFile = async (
89
92
  }
90
93
  };
91
94
 
95
+ await sleep(delayTime);
96
+ if (await checkCanceled()) {
97
+ if (fs.existsSync(dest)) {
98
+ fs.truncateSync(dest, 0);
99
+ fs.rmSync(dest, { force: true });
100
+ }
101
+ return CANCEL;
102
+ }
92
103
  const response = await axios({
93
104
  url,
94
105
  headers: merge(context?.headers || {}, {
@@ -111,8 +122,8 @@ const downloadFile = async (
111
122
  // 每 2 秒检查一次, db-cache 里是否标记了取消, 如果有, 则取消下载
112
123
  if (Date.now() - t > 2000) {
113
124
  t = Date.now();
114
- checkCanceled().then((isCancelled) => {
115
- if (isCancelled) {
125
+ checkCanceled().then((cancelled) => {
126
+ if (cancelled) {
116
127
  cancelDownload();
117
128
  }
118
129
  });
@@ -141,6 +152,10 @@ const downloadFile = async (
141
152
  throw new Error('download incomplete');
142
153
  }
143
154
 
155
+ if (isCancelled) {
156
+ return CANCEL;
157
+ }
158
+
144
159
  return dest;
145
160
  }, timeout);
146
161
  } catch (err) {
@@ -161,7 +161,11 @@ module.exports = async ({
161
161
  }
162
162
  }
163
163
  };
164
- return tryWithTimeout(fn, timeout);
164
+ try {
165
+ return await tryWithTimeout(fn, timeout);
166
+ } catch (error) {
167
+ throw new Error(`service not ready within ${Math.ceil(timeout / ONE_SECOND)} seconds, error: ${error?.message}`);
168
+ }
165
169
  };
166
170
 
167
171
  module.exports.dial = dial;
package/lib/get-ip.js CHANGED
@@ -38,9 +38,15 @@ const ensureFormat = ({ internal, external, internalV6, externalV6 }) => ({
38
38
  * externalV6: string,
39
39
  * }>}
40
40
  */
41
- const getIP = async ({ includeV6 = false, timeout = 5000, includeExternal = true } = {}) => {
41
+ const getIP = async ({
42
+ includeV6 = false,
43
+ timeout = 5000,
44
+ includeExternal = true,
45
+ checkIsEC2 = isEC2,
46
+ checkIsGCP = gcp.isInGCP,
47
+ } = {}) => {
42
48
  try {
43
- const [inEc2, inGCP] = await Promise.all([isEC2(), gcp.isInGCP()]);
49
+ const [inEc2, inGCP] = await Promise.all([checkIsEC2(), checkIsGCP()]);
44
50
  debug('check env', { inEc2, inGCP });
45
51
 
46
52
  if (process.env.ABT_NODE_HOST) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.54-beta-20251024-030947-6f2889bf",
6
+ "version": "1.16.54-beta-20251027-105624-dfa978f8",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,14 +18,14 @@
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.54-beta-20251024-030947-6f2889bf",
22
- "@abtnode/db-cache": "1.16.54-beta-20251024-030947-6f2889bf",
21
+ "@abtnode/constant": "1.16.54-beta-20251027-105624-dfa978f8",
22
+ "@abtnode/db-cache": "1.16.54-beta-20251027-105624-dfa978f8",
23
23
  "@arcblock/did": "^1.26.3",
24
24
  "@arcblock/event-hub": "^1.26.3",
25
25
  "@arcblock/pm2": "^6.0.12",
26
- "@blocklet/constant": "1.16.54-beta-20251024-030947-6f2889bf",
26
+ "@blocklet/constant": "1.16.54-beta-20251027-105624-dfa978f8",
27
27
  "@blocklet/error": "^0.2.5",
28
- "@blocklet/meta": "1.16.54-beta-20251024-030947-6f2889bf",
28
+ "@blocklet/meta": "1.16.54-beta-20251027-105624-dfa978f8",
29
29
  "@blocklet/xss": "^0.2.12",
30
30
  "@ocap/client": "^1.26.3",
31
31
  "@ocap/mcrypto": "^1.26.3",
@@ -88,8 +88,7 @@
88
88
  "cookie-signature": "^1.2.2",
89
89
  "detect-port": "^1.5.1",
90
90
  "express": "^4.18.2",
91
- "fs-extra": "^11.2.0",
92
- "jest": "^29.7.0"
91
+ "fs-extra": "^11.2.0"
93
92
  },
94
- "gitHead": "73e5a3a80b82a2a7c62d42fdc08207b28e67633e"
93
+ "gitHead": "03b0bb26807c6c427ca700f48566ecc913f1353e"
95
94
  }