@iflyrpa/share 0.1.0 → 0.1.1
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/dist/index.js +31 -12
- package/dist/index.mjs +31 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -169,6 +169,7 @@ function ensureFile(filePath) {
|
|
|
169
169
|
}
|
|
170
170
|
});
|
|
171
171
|
}
|
|
172
|
+
const DOWNLOAD_BASE_TIMEOUT = 60000;
|
|
172
173
|
async function downloadImage(url, savePath, retries = 3) {
|
|
173
174
|
const RETRYABLE_CODES = new Set([
|
|
174
175
|
"ECONNRESET",
|
|
@@ -176,20 +177,25 @@ async function downloadImage(url, savePath, retries = 3) {
|
|
|
176
177
|
"EAI_AGAIN",
|
|
177
178
|
"ENOTFOUND"
|
|
178
179
|
]);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
180
|
+
const maxAttempts = retries;
|
|
181
|
+
let lastErr;
|
|
182
|
+
for(let attempt = 1; attempt <= maxAttempts; attempt++){
|
|
183
|
+
const timeout = attempt * DOWNLOAD_BASE_TIMEOUT;
|
|
184
|
+
try {
|
|
185
|
+
return await downloadImageOnce(url, savePath, timeout);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
var _err_extra;
|
|
188
|
+
lastErr = err;
|
|
189
|
+
const errCode = (null == err ? void 0 : err.code) ?? (null == err ? void 0 : null === (_err_extra = err.extra) || void 0 === _err_extra ? void 0 : _err_extra.code);
|
|
190
|
+
const retryable = RETRYABLE_CODES.has(errCode) || (null == err ? void 0 : err.code) === 500;
|
|
191
|
+
if (attempt >= maxAttempts || !retryable) throw err;
|
|
192
|
+
console.warn(`downloadImage 失败,已尝试 ${attempt}/${maxAttempts} 次,下次超时 ${(attempt + 1) * DOWNLOAD_BASE_TIMEOUT / 1000}s,url: ${url},原因:`, (null == err ? void 0 : err.message) ?? err);
|
|
186
193
|
await new Promise((r)=>setTimeout(r, 1000));
|
|
187
|
-
return downloadImage(url, savePath, retries - 1);
|
|
188
194
|
}
|
|
189
|
-
throw err;
|
|
190
195
|
}
|
|
196
|
+
throw lastErr;
|
|
191
197
|
}
|
|
192
|
-
async function downloadImageOnce(url, savePath) {
|
|
198
|
+
async function downloadImageOnce(url, savePath, timeout = DOWNLOAD_BASE_TIMEOUT) {
|
|
193
199
|
await ensureFile(savePath);
|
|
194
200
|
return new Promise((resolve, reject)=>{
|
|
195
201
|
const handleResponse = (response)=>{
|
|
@@ -271,11 +277,24 @@ async function downloadImageOnce(url, savePath) {
|
|
|
271
277
|
};
|
|
272
278
|
const requestFn = url.startsWith("http://") ? external_node_http_default().get : external_node_https_default().get;
|
|
273
279
|
const req = requestFn(url, handleResponse);
|
|
274
|
-
|
|
280
|
+
let remoteAddress = "";
|
|
281
|
+
req.on("socket", (socket)=>{
|
|
282
|
+
socket.on("connect", ()=>{
|
|
283
|
+
remoteAddress = `${socket.remoteAddress ?? ""}:${socket.remotePort ?? ""}`;
|
|
284
|
+
});
|
|
285
|
+
if (socket.remoteAddress) remoteAddress = `${socket.remoteAddress}:${socket.remotePort ?? ""}`;
|
|
286
|
+
});
|
|
287
|
+
req.setTimeout(timeout, ()=>{
|
|
275
288
|
req.destroy();
|
|
289
|
+
const remoteInfo = remoteAddress || "未建立连接";
|
|
290
|
+
console.warn(`图片下载超时 (${timeout / 1000}s),远端IP: ${remoteInfo},Url:${url}`);
|
|
276
291
|
reject({
|
|
277
292
|
code: 500,
|
|
278
|
-
message: `图片下载超时 (${url}
|
|
293
|
+
message: `图片下载超时 (${timeout / 1000}s) Url:${url},请检查网络连接或稍后重试! 远端ip: ${remoteInfo}`,
|
|
294
|
+
extra: {
|
|
295
|
+
url,
|
|
296
|
+
remoteAddress: remoteInfo
|
|
297
|
+
}
|
|
279
298
|
});
|
|
280
299
|
});
|
|
281
300
|
req.on("error", handleError);
|
package/dist/index.mjs
CHANGED
|
@@ -106,6 +106,7 @@ function ensureFile(filePath) {
|
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
|
+
const DOWNLOAD_BASE_TIMEOUT = 60000;
|
|
109
110
|
async function downloadImage(url, savePath, retries = 3) {
|
|
110
111
|
const RETRYABLE_CODES = new Set([
|
|
111
112
|
"ECONNRESET",
|
|
@@ -113,20 +114,25 @@ async function downloadImage(url, savePath, retries = 3) {
|
|
|
113
114
|
"EAI_AGAIN",
|
|
114
115
|
"ENOTFOUND"
|
|
115
116
|
]);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
117
|
+
const maxAttempts = retries;
|
|
118
|
+
let lastErr;
|
|
119
|
+
for(let attempt = 1; attempt <= maxAttempts; attempt++){
|
|
120
|
+
const timeout = attempt * DOWNLOAD_BASE_TIMEOUT;
|
|
121
|
+
try {
|
|
122
|
+
return await downloadImageOnce(url, savePath, timeout);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
var _err_extra;
|
|
125
|
+
lastErr = err;
|
|
126
|
+
const errCode = (null == err ? void 0 : err.code) ?? (null == err ? void 0 : null === (_err_extra = err.extra) || void 0 === _err_extra ? void 0 : _err_extra.code);
|
|
127
|
+
const retryable = RETRYABLE_CODES.has(errCode) || (null == err ? void 0 : err.code) === 500;
|
|
128
|
+
if (attempt >= maxAttempts || !retryable) throw err;
|
|
129
|
+
console.warn(`downloadImage 失败,已尝试 ${attempt}/${maxAttempts} 次,下次超时 ${(attempt + 1) * DOWNLOAD_BASE_TIMEOUT / 1000}s,url: ${url},原因:`, (null == err ? void 0 : err.message) ?? err);
|
|
123
130
|
await new Promise((r)=>setTimeout(r, 1000));
|
|
124
|
-
return downloadImage(url, savePath, retries - 1);
|
|
125
131
|
}
|
|
126
|
-
throw err;
|
|
127
132
|
}
|
|
133
|
+
throw lastErr;
|
|
128
134
|
}
|
|
129
|
-
async function downloadImageOnce(url, savePath) {
|
|
135
|
+
async function downloadImageOnce(url, savePath, timeout = DOWNLOAD_BASE_TIMEOUT) {
|
|
130
136
|
await ensureFile(savePath);
|
|
131
137
|
return new Promise((resolve, reject)=>{
|
|
132
138
|
const handleResponse = (response)=>{
|
|
@@ -208,11 +214,24 @@ async function downloadImageOnce(url, savePath) {
|
|
|
208
214
|
};
|
|
209
215
|
const requestFn = url.startsWith("http://") ? __WEBPACK_EXTERNAL_MODULE_node_http_2dc67212__["default"].get : __WEBPACK_EXTERNAL_MODULE_node_https_626f33a7__["default"].get;
|
|
210
216
|
const req = requestFn(url, handleResponse);
|
|
211
|
-
|
|
217
|
+
let remoteAddress = "";
|
|
218
|
+
req.on("socket", (socket)=>{
|
|
219
|
+
socket.on("connect", ()=>{
|
|
220
|
+
remoteAddress = `${socket.remoteAddress ?? ""}:${socket.remotePort ?? ""}`;
|
|
221
|
+
});
|
|
222
|
+
if (socket.remoteAddress) remoteAddress = `${socket.remoteAddress}:${socket.remotePort ?? ""}`;
|
|
223
|
+
});
|
|
224
|
+
req.setTimeout(timeout, ()=>{
|
|
212
225
|
req.destroy();
|
|
226
|
+
const remoteInfo = remoteAddress || "未建立连接";
|
|
227
|
+
console.warn(`图片下载超时 (${timeout / 1000}s),远端IP: ${remoteInfo},Url:${url}`);
|
|
213
228
|
reject({
|
|
214
229
|
code: 500,
|
|
215
|
-
message: `图片下载超时 (${url}
|
|
230
|
+
message: `图片下载超时 (${timeout / 1000}s) Url:${url},请检查网络连接或稍后重试! 远端ip: ${remoteInfo}`,
|
|
231
|
+
extra: {
|
|
232
|
+
url,
|
|
233
|
+
remoteAddress: remoteInfo
|
|
234
|
+
}
|
|
216
235
|
});
|
|
217
236
|
});
|
|
218
237
|
req.on("error", handleError);
|