@iflyrpa/actions 3.0.1 → 3.0.2

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 CHANGED
@@ -5490,7 +5490,7 @@ var __webpack_exports__ = {};
5490
5490
  const package_json_namespaceObject = require("@iflyrpa/share/package.json");
5491
5491
  var package_json_default = /*#__PURE__*/ __webpack_require__.n(package_json_namespaceObject);
5492
5492
  var package_namespaceObject = {
5493
- i8: "3.0.0"
5493
+ i8: "3.0.1"
5494
5494
  };
5495
5495
  const share_namespaceObject = require("@iflyrpa/share");
5496
5496
  const external_node_fs_namespaceObject = require("node:fs");
@@ -5628,6 +5628,9 @@ var __webpack_exports__ = {};
5628
5628
  case "ERR_BAD_REQUEST":
5629
5629
  _message = "请求出现错误,请检查请求参数!";
5630
5630
  break;
5631
+ case "ERR_BAD_RESPONSE":
5632
+ _message = `服务器响应异常 (${error.response?.status ?? "unknown"}),请稍后重试!`;
5633
+ break;
5631
5634
  case "ERR_CANCELED":
5632
5635
  errorResponse.code = 414;
5633
5636
  _message = "请求连接超时,请稍候重试!";
@@ -5678,6 +5681,9 @@ var __webpack_exports__ = {};
5678
5681
  const isRetry = [
5679
5682
  0,
5680
5683
  500,
5684
+ 502,
5685
+ 503,
5686
+ 504,
5681
5687
  599
5682
5688
  ].includes(handledError.code);
5683
5689
  if (Rtimes < retries && isRetry) {
@@ -14006,7 +14012,7 @@ var __webpack_exports__ = {};
14006
14012
  this.authCache = null;
14007
14013
  this.authExpireTime = 0;
14008
14014
  this.AUTH_REFRESH_BUFFER = 300000;
14009
- this.CONCURRENT_LIMIT = 5;
14015
+ this.CONCURRENT_LIMIT = 10;
14010
14016
  this.UPLOAD_DELAY = 1000;
14011
14017
  this.UPLOAD_TIMEOUT = 300000;
14012
14018
  this.TOS_UPLOAD_TIMEOUT = 600000;
@@ -14035,16 +14041,17 @@ var __webpack_exports__ = {};
14035
14041
  const aBogus = imageUploader_getABogus(`https://creator.douyin.com/web/api/media/upload/auth/v5/?${queryString}`, {}, this.headers["user-agent"]);
14036
14042
  this.queryParams.a_bogus = aBogus;
14037
14043
  const newQueryString = new URLSearchParams(this.queryParams).toString();
14038
- const uploadAuth = await this.proxyHttp.api({
14039
- method: "get",
14040
- url: `https://creator.douyin.com/web/api/media/upload/auth/v5/?${newQueryString}`,
14041
- headers: {
14042
- ...this.headers,
14043
- "Content-Type": "application/json"
14044
- }
14045
- }, {
14046
- timeout: 300000
14047
- });
14044
+ const uploadAuth = await this.withRetry(()=>this.proxyHttp.api({
14045
+ method: "get",
14046
+ url: `https://creator.douyin.com/web/api/media/upload/auth/v5/?${newQueryString}`,
14047
+ headers: {
14048
+ ...this.headers,
14049
+ "Content-Type": "application/json"
14050
+ }
14051
+ }, {
14052
+ timeout: 300000,
14053
+ retries: 0
14054
+ }), 3, 1000, "获取上传凭证");
14048
14055
  if (!uploadAuth.auth) throw new Error(uploadAuth.status_msg || "获取上传权限失败");
14049
14056
  try {
14050
14057
  const authData = JSON.parse(uploadAuth.auth);
@@ -14084,19 +14091,19 @@ var __webpack_exports__ = {};
14084
14091
  };
14085
14092
  const result = generateAuthorization(addrParamsConfig).authorization;
14086
14093
  const addrParamsQueryString = canonicalQueryString(addrParamsConfig.params);
14087
- const response = await this.proxyHttp.api({
14088
- method: "get",
14089
- url: `https://imagex.bytedanceapi.com/?${addrParamsQueryString}`,
14090
- headers: {
14091
- ...this.headers,
14092
- Authorization: result,
14093
- "Content-Type": "application/json",
14094
- "X-amz-date": amzDate,
14095
- "X-amz-security-token": authData.SessionToken
14096
- }
14097
- }, {
14098
- timeout: this.UPLOAD_TIMEOUT
14099
- });
14094
+ const response = await this.withRetry(()=>this.proxyHttp.api({
14095
+ method: "get",
14096
+ url: `https://imagex.bytedanceapi.com/?${addrParamsQueryString}`,
14097
+ headers: {
14098
+ ...this.headers,
14099
+ Authorization: result,
14100
+ "Content-Type": "application/json",
14101
+ "X-amz-date": amzDate,
14102
+ "X-amz-security-token": authData.SessionToken
14103
+ }
14104
+ }, {
14105
+ timeout: this.UPLOAD_TIMEOUT
14106
+ }), 3, 1000, "获取上传地址");
14100
14107
  if (!response) throw new Error("获取上传地址失败: 响应为空");
14101
14108
  if (response.ResponseMetadata?.Error) throw new Error(`ImageX错误: ${response.ResponseMetadata.Error.Code} - ${response.ResponseMetadata.Error.Message}`);
14102
14109
  if (!response.Result) {
@@ -14127,23 +14134,24 @@ var __webpack_exports__ = {};
14127
14134
  console.log(`上传至: ${uploadUrl.substring(0, 80)}...`);
14128
14135
  console.log(`文件大小: ${imageInfo.size} bytes, CRC32: ${imageInfo.md5}`);
14129
14136
  try {
14130
- const uploadImgResp = await this.proxyHttp.api({
14131
- method: "post",
14132
- url: uploadUrl,
14133
- headers: {
14134
- ...this.headers,
14135
- Authorization: uploadAddr.auth,
14136
- "Content-crc32": imageInfo.md5,
14137
- "Content-Type": "application/octet-stream",
14138
- "Content-Length": imageInfo.size.toString()
14139
- },
14140
- data: fileBuffer,
14141
- httpAgent: this.uploadAgent,
14142
- httpsAgent: this.uploadAgent,
14143
- timeout: this.TOS_UPLOAD_TIMEOUT,
14144
- maxBodyLength: 1 / 0,
14145
- maxContentLength: 1 / 0
14146
- });
14137
+ const uploadImgResp = await this.withRetry(()=>this.proxyHttp.api({
14138
+ method: "post",
14139
+ url: uploadUrl,
14140
+ headers: {
14141
+ ...this.headers,
14142
+ Authorization: uploadAddr.auth,
14143
+ "Content-crc32": imageInfo.md5,
14144
+ "Content-Type": "application/octet-stream",
14145
+ "Content-Length": imageInfo.size.toString()
14146
+ },
14147
+ data: fileBuffer,
14148
+ httpAgent: this.uploadAgent,
14149
+ httpsAgent: this.uploadAgent,
14150
+ maxBodyLength: 1 / 0,
14151
+ maxContentLength: 1 / 0
14152
+ }, {
14153
+ timeout: this.TOS_UPLOAD_TIMEOUT
14154
+ }), 5, 3000, "TOS上传");
14147
14155
  console.log("TOS 上传响应:", uploadImgResp);
14148
14156
  const isSuccess = uploadImgResp && (2000 === uploadImgResp.code || 200 === uploadImgResp.status || 200 === uploadImgResp.statusCode || uploadImgResp.data && uploadImgResp.data.crc32 || "Success" === uploadImgResp.message);
14149
14157
  if (!isSuccess) throw new Error(`上传失败: ${JSON.stringify(uploadImgResp)}`);
@@ -14164,20 +14172,22 @@ var __webpack_exports__ = {};
14164
14172
  async processSingleImage(imageUrl, index, total) {
14165
14173
  console.log(`\n[${index + 1}/${total}] 处理图片: ${imageUrl.substring(0, 50)}...`);
14166
14174
  const fileName = (0, share_namespaceObject.getFilenameFromUrl)(imageUrl);
14167
- const localPath = external_node_path_default().join(this.tmpCachePath, fileName);
14175
+ const localPath = external_node_path_default().resolve(this.tmpCachePath, fileName);
14168
14176
  try {
14169
14177
  await (0, share_namespaceObject.downloadImage)(imageUrl, localPath);
14170
14178
  console.log(`图片下载完成: ${localPath}`);
14179
+ const imageInfo = await this.getImageInfo(localPath);
14180
+ console.log(`图片信息: ${imageInfo.width}x${imageInfo.height}, ${imageInfo.size} bytes`);
14181
+ const auth = await this.getValidAuth();
14182
+ const uploadAddr = await this.getUploadAddress(auth);
14183
+ const result = await this.uploadToTOS(imageInfo, uploadAddr);
14184
+ this.safeDeleteFile(localPath);
14185
+ return result;
14171
14186
  } catch (error) {
14172
- throw new Error(`下载图片失败: ${imageUrl}`);
14173
- }
14174
- const imageInfo = await this.getImageInfo(localPath);
14175
- console.log(`图片信息: ${imageInfo.width}x${imageInfo.height}, ${imageInfo.size} bytes`);
14176
- const auth = await this.getValidAuth();
14177
- const uploadAddr = await this.getUploadAddress(auth);
14178
- const result = await this.uploadToTOS(imageInfo, uploadAddr);
14179
- this.safeDeleteFile(localPath);
14180
- return result;
14187
+ console.error(`[${index + 1}/${total}] 图片上传失败,已跳过: ${imageUrl}`, error?.message ?? error);
14188
+ this.safeDeleteFile(localPath);
14189
+ return null;
14190
+ }
14181
14191
  }
14182
14192
  async getImageInfo(localPath) {
14183
14193
  const stats = external_node_fs_default().statSync(localPath);
@@ -14210,13 +14220,11 @@ var __webpack_exports__ = {};
14210
14220
  const globalIndex = i + idx;
14211
14221
  return this.processSingleImage(url, globalIndex, total);
14212
14222
  });
14213
- try {
14214
- const batchResults = await Promise.all(batchPromises);
14215
- results.push(...batchResults);
14216
- } catch (error) {
14217
- console.error(`批次 ${batchIndex} 上传失败:`, error);
14218
- throw error;
14219
- }
14223
+ const batchResults = await Promise.all(batchPromises);
14224
+ const successResults = batchResults.filter((r)=>null !== r);
14225
+ results.push(...successResults);
14226
+ const failedCount = batchResults.length - successResults.length;
14227
+ if (failedCount > 0) console.warn(`批次 ${batchIndex} 中有 ${failedCount} 张图片上传失败`);
14220
14228
  if (i + this.CONCURRENT_LIMIT < total) {
14221
14229
  console.log(`等待 ${this.UPLOAD_DELAY}ms...`);
14222
14230
  await this.delay(this.UPLOAD_DELAY);
@@ -14228,6 +14236,7 @@ var __webpack_exports__ = {};
14228
14236
  async uploadCover(coverUrl) {
14229
14237
  console.log("\n========== 上传封面 ==========\n");
14230
14238
  const result = await this.processSingleImage(coverUrl, 0, 1);
14239
+ if (!result) console.error(`封面图上传失败,已跳过: ${coverUrl}`);
14231
14240
  return result;
14232
14241
  }
14233
14242
  safeDeleteFile(filePath) {
@@ -14241,6 +14250,17 @@ var __webpack_exports__ = {};
14241
14250
  delay(ms) {
14242
14251
  return new Promise((resolve)=>setTimeout(resolve, ms));
14243
14252
  }
14253
+ async withRetry(fn, retries = 3, delayMs = 1000, label = "") {
14254
+ for(let attempt = 1; attempt <= retries; attempt++)try {
14255
+ return await fn();
14256
+ } catch (err) {
14257
+ if (attempt === retries) throw err;
14258
+ const waitTime = delayMs * attempt;
14259
+ console.warn(`${label} 第 ${attempt} 次失败,${waitTime}ms 后重试:`, err?.message ?? err);
14260
+ await this.delay(waitTime);
14261
+ }
14262
+ throw new Error("unreachable");
14263
+ }
14244
14264
  destroy() {
14245
14265
  this.uploadAgent.destroy();
14246
14266
  }
@@ -14437,14 +14457,16 @@ var __webpack_exports__ = {};
14437
14457
  const uploader = new DouyinImageUploader(proxyHttp, headers, publishParams, tmpCachePath);
14438
14458
  if (params.coverImage) {
14439
14459
  const cover = await uploader.uploadCover(params.coverImage);
14440
- publishData.item.common.images.push({
14441
- uri: cover.uri,
14442
- width: cover.width,
14443
- height: cover.height
14444
- });
14445
- publishData.item.cover = {
14446
- poster: cover.uri
14447
- };
14460
+ if (cover) {
14461
+ publishData.item.common.images.push({
14462
+ uri: cover.uri,
14463
+ width: cover.width,
14464
+ height: cover.height
14465
+ });
14466
+ publishData.item.cover = {
14467
+ poster: cover.uri
14468
+ };
14469
+ }
14448
14470
  }
14449
14471
  if (params.banners && params.banners.length > 0) {
14450
14472
  const banners = params.banners.filter((i)=>i !== params.coverImage);
@@ -14470,11 +14492,15 @@ var __webpack_exports__ = {};
14470
14492
  }
14471
14493
  if (!response || !response.data) return;
14472
14494
  const responseData = response.data;
14473
- if (response && responseData?.status_code && 0 !== responseData.status_code) return {
14474
- code: responseData?.status_code,
14475
- message: responseData.status_msg || "文章发布异常,请稍后重试。",
14476
- data: responseData
14477
- };
14495
+ if (response && responseData?.status_code && 0 !== responseData.status_code) {
14496
+ const errorCode = 4 === responseData.status_code ? 500 : responseData.status_code;
14497
+ if (4 === responseData.status_code) task.logger.warn(`抖音服务器错误 status_code: 4,映射为 500 触发重试。原始响应: ${JSON.stringify(responseData)}`);
14498
+ return {
14499
+ code: errorCode,
14500
+ message: responseData.status_msg || "文章发布异常,请稍后重试。",
14501
+ data: responseData
14502
+ };
14503
+ }
14478
14504
  });
14479
14505
  task._timerRecord.PrePublish = Date.now();
14480
14506
  if (MockPublish) {
@@ -14557,7 +14583,9 @@ var __webpack_exports__ = {};
14557
14583
  "x-tt-session-dtrait": sessionDtrait || ""
14558
14584
  }
14559
14585
  }, {
14560
- timeout: 60000
14586
+ timeout: 60000,
14587
+ retries: 3,
14588
+ retryDelay: 2000
14561
14589
  });
14562
14590
  reportLogger({
14563
14591
  token: params.huiwenToken || "",
@@ -14570,9 +14598,8 @@ var __webpack_exports__ = {};
14570
14598
  platform: "douyin",
14571
14599
  publishParams: publishData
14572
14600
  });
14573
- console.log("publishResult", JSON.stringify(publishResult));
14574
- task.logger.warn("发布数据");
14575
- task.logger.warn(JSON.stringify(publishData));
14601
+ task.logger.warn("发布结果");
14602
+ task.logger.warn(JSON.stringify(publishResult));
14576
14603
  const isSuccess = 0 === publishResult.status_code;
14577
14604
  const message = `图文发布${isSuccess ? "成功" : `失败,原因:${publishResult.status_msg} ${decision?.verify_title} ${decision?.verify_desc}`}${task.debug ? ` ${http.proxyInfo}` : ""}`;
14578
14605
  const data = isSuccess ? publishResult.item_id || "" : "";