base.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. var __read = (this && this.__read) || function (o, n) {
  49. var m = typeof Symbol === "function" && o[Symbol.iterator];
  50. if (!m) return o;
  51. var i = m.call(o), r, ar = [], e;
  52. try {
  53. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  54. }
  55. catch (error) { e = { error: error }; }
  56. finally {
  57. try {
  58. if (r && !r.done && (m = i["return"])) m.call(i);
  59. }
  60. finally { if (e) throw e.error; }
  61. }
  62. return ar;
  63. };
  64. var __spread = (this && this.__spread) || function () {
  65. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  66. return ar;
  67. };
  68. import { QiniuErrorName, QiniuError, QiniuRequestError } from '../errors';
  69. import * as utils from '../utils';
  70. export var DEFAULT_CHUNK_SIZE = 4; // 单位 MB
  71. // code 信息地址 https://developer.qiniu.com/kodo/3928/error-responses
  72. export var FREEZE_CODE_LIST = [0, 502, 503, 504, 599]; // 将会冻结当前 host 的 code
  73. export var RETRY_CODE_LIST = __spread(FREEZE_CODE_LIST, [612]); // 会进行重试的 code
  74. var GB = Math.pow(1024, 3);
  75. var Base = /** @class */ (function () {
  76. function Base(options, handlers, hostPool, logger) {
  77. this.hostPool = hostPool;
  78. this.logger = logger;
  79. this.aborted = false;
  80. this.retryCount = 0;
  81. this.xhrList = [];
  82. this.config = options.config;
  83. logger.info('config inited.', this.config);
  84. this.putExtra = __assign({ fname: '' }, options.putExtra);
  85. logger.info('putExtra inited.', this.putExtra);
  86. this.key = options.key;
  87. this.file = options.file;
  88. this.token = options.token;
  89. this.onData = handlers.onData;
  90. this.onError = handlers.onError;
  91. this.onComplete = handlers.onComplete;
  92. try {
  93. var putPolicy = utils.getPutPolicy(this.token);
  94. this.bucketName = putPolicy.bucketName;
  95. this.assessKey = putPolicy.assessKey;
  96. }
  97. catch (error) {
  98. logger.error('get putPolicy from token failed.', error);
  99. this.onError(error);
  100. }
  101. }
  102. // 检查并更新 upload host
  103. Base.prototype.checkAndUpdateUploadHost = function () {
  104. return __awaiter(this, void 0, void 0, function () {
  105. var newHost;
  106. return __generator(this, function (_a) {
  107. switch (_a.label) {
  108. case 0:
  109. // 从 hostPool 中获取一个可用的 host 挂载在 this
  110. this.logger.info('get available upload host.');
  111. return [4 /*yield*/, this.hostPool.getUp(this.assessKey, this.bucketName, this.config.upprotocol)];
  112. case 1:
  113. newHost = _a.sent();
  114. if (newHost == null) {
  115. throw new QiniuError(QiniuErrorName.NotAvailableUploadHost, 'no available upload host.');
  116. }
  117. if (this.uploadHost != null && this.uploadHost.host !== newHost.host) {
  118. this.logger.warn("host switches from " + this.uploadHost.host + " to " + newHost.host + ".");
  119. }
  120. else {
  121. this.logger.info("use host " + newHost.host + ".");
  122. }
  123. this.uploadHost = newHost;
  124. return [2 /*return*/];
  125. }
  126. });
  127. });
  128. };
  129. // 检查并解冻当前的 host
  130. Base.prototype.checkAndUnfreezeHost = function () {
  131. this.logger.info('check unfreeze host.');
  132. if (this.uploadHost != null && this.uploadHost.isFrozen()) {
  133. this.logger.warn(this.uploadHost.host + " will be unfrozen.");
  134. this.uploadHost.unfreeze();
  135. }
  136. };
  137. // 检查并更新冻结当前的 host
  138. Base.prototype.checkAndFreezeHost = function (error) {
  139. this.logger.info('check freeze host.');
  140. if (error instanceof QiniuRequestError && this.uploadHost != null) {
  141. if (FREEZE_CODE_LIST.includes(error.code)) {
  142. this.logger.warn(this.uploadHost.host + " will be temporarily frozen.");
  143. this.uploadHost.freeze();
  144. }
  145. }
  146. };
  147. Base.prototype.handleError = function (error) {
  148. this.logger.error(error.message);
  149. this.onError(error);
  150. };
  151. /**
  152. * @returns Promise 返回结果与上传最终状态无关,状态信息请通过 [Subscriber] 获取。
  153. * @description 上传文件,状态信息请通过 [Subscriber] 获取。
  154. */
  155. Base.prototype.putFile = function () {
  156. return __awaiter(this, void 0, void 0, function () {
  157. var result, err_1, notReachRetryCount, needRetry;
  158. return __generator(this, function (_a) {
  159. switch (_a.label) {
  160. case 0:
  161. this.aborted = false;
  162. if (!this.putExtra.fname) {
  163. this.logger.info('use file.name as fname.');
  164. this.putExtra.fname = this.file.name;
  165. }
  166. if (this.file.size > 10000 * GB) {
  167. this.handleError(new QiniuError(QiniuErrorName.InvalidFile, 'file size exceed maximum value 10000G'));
  168. return [2 /*return*/];
  169. }
  170. if (this.putExtra.customVars) {
  171. if (!utils.isCustomVarsValid(this.putExtra.customVars)) {
  172. this.handleError(new QiniuError(QiniuErrorName.InvalidCustomVars,
  173. // FIXME: width => with
  174. 'customVars key should start width x:'));
  175. return [2 /*return*/];
  176. }
  177. }
  178. if (this.putExtra.metadata) {
  179. if (!utils.isMetaDataValid(this.putExtra.metadata)) {
  180. this.handleError(new QiniuError(QiniuErrorName.InvalidMetadata, 'metadata key should start with x-qn-meta-'));
  181. return [2 /*return*/];
  182. }
  183. }
  184. _a.label = 1;
  185. case 1:
  186. _a.trys.push([1, 4, , 5]);
  187. this.uploadAt = new Date().getTime();
  188. return [4 /*yield*/, this.checkAndUpdateUploadHost()];
  189. case 2:
  190. _a.sent();
  191. return [4 /*yield*/, this.run()];
  192. case 3:
  193. result = _a.sent();
  194. this.onComplete(result.data);
  195. this.checkAndUnfreezeHost();
  196. this.sendLog(result.reqId, 200);
  197. return [2 /*return*/];
  198. case 4:
  199. err_1 = _a.sent();
  200. if (this.aborted) {
  201. this.logger.warn('upload is aborted.');
  202. this.sendLog('', -2);
  203. return [2 /*return*/];
  204. }
  205. this.clear();
  206. this.logger.error(err_1);
  207. if (err_1 instanceof QiniuRequestError) {
  208. this.sendLog(err_1.reqId, err_1.code);
  209. // 检查并冻结当前的 host
  210. this.checkAndFreezeHost(err_1);
  211. notReachRetryCount = ++this.retryCount <= this.config.retryCount;
  212. needRetry = RETRY_CODE_LIST.includes(err_1.code);
  213. // 以下条件满足其中之一则会进行重新上传:
  214. // 1. 满足 needRetry 的条件且 retryCount 不为 0
  215. // 2. uploadId 无效时在 resume 里会清除本地数据,并且这里触发重新上传
  216. if (needRetry && notReachRetryCount) {
  217. this.logger.warn("error auto retry: " + this.retryCount + "/" + this.config.retryCount + ".");
  218. this.putFile();
  219. return [2 /*return*/];
  220. }
  221. }
  222. this.onError(err_1);
  223. return [3 /*break*/, 5];
  224. case 5: return [2 /*return*/];
  225. }
  226. });
  227. });
  228. };
  229. Base.prototype.clear = function () {
  230. this.xhrList.forEach(function (xhr) {
  231. xhr.onreadystatechange = null;
  232. xhr.abort();
  233. });
  234. this.xhrList = [];
  235. this.logger.info('cleanup uploading xhr.');
  236. };
  237. Base.prototype.stop = function () {
  238. this.logger.info('aborted.');
  239. this.clear();
  240. this.aborted = true;
  241. };
  242. Base.prototype.addXhr = function (xhr) {
  243. this.xhrList.push(xhr);
  244. };
  245. Base.prototype.sendLog = function (reqId, code) {
  246. var _a, _b;
  247. this.logger.report({
  248. code: code,
  249. reqId: reqId,
  250. remoteIp: '',
  251. upType: 'jssdk-h5',
  252. size: this.file.size,
  253. time: Math.floor(this.uploadAt / 1000),
  254. port: utils.getPortFromUrl((_a = this.uploadHost) === null || _a === void 0 ? void 0 : _a.getUrl()),
  255. host: utils.getDomainFromUrl((_b = this.uploadHost) === null || _b === void 0 ? void 0 : _b.getUrl()),
  256. bytesSent: this.progress ? this.progress.total.loaded : 0,
  257. duration: Math.floor((new Date().getTime() - this.uploadAt) / 1000)
  258. });
  259. };
  260. Base.prototype.getProgressInfoItem = function (loaded, size, fromCache) {
  261. return __assign({ size: size,
  262. loaded: loaded, percent: loaded / size * 100 }, (fromCache == null ? {} : { fromCache: fromCache }));
  263. };
  264. return Base;
  265. }());
  266. export default Base;
  267. //# sourceMappingURL=base.js.map