LongPollingTransport.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // 版权所有(c).NET基金会。保留所有权利。
  2. // 在2.0版Apache许可下授权。有关许可证信息,请参见项目根目录中的License.txt。
  3. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  4. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  5. return new (P || (P = Promise))(function (resolve, reject) {
  6. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  7. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  8. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  9. step((generator = generator.apply(thisArg, _arguments || [])).next());
  10. });
  11. };
  12. var __generator = (this && this.__generator) || function (thisArg, body) {
  13. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  14. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  15. function verb(n) { return function (v) { return step([n, v]); }; }
  16. function step(op) {
  17. if (f) throw new TypeError("Generator is already executing.");
  18. while (_) try {
  19. 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;
  20. if (y = 0, t) op = [op[0] & 2, t.value];
  21. switch (op[0]) {
  22. case 0: case 1: t = op; break;
  23. case 4: _.label++; return { value: op[1], done: false };
  24. case 5: _.label++; y = op[1]; op = [0]; continue;
  25. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  26. default:
  27. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  28. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  29. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  30. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  31. if (t[2]) _.ops.pop();
  32. _.trys.pop(); continue;
  33. }
  34. op = body.call(thisArg, _);
  35. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  36. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  37. }
  38. };
  39. import { AbortController } from "./AbortController";
  40. import { HttpError, TimeoutError } from "./Errors";
  41. import { LogLevel } from "./ILogger";
  42. import { TransferFormat } from "./ITransport";
  43. import { Arg, getDataDetail, sendMessage } from "./Utils";
  44. import { ResponseType } from "./wx-request/model/ResponseType";
  45. import { NullLogger } from './Loggers';
  46. import DefaultRequest from "./DefualtRequest";
  47. // Not exported from 'index', this type is internal.
  48. /**
  49. * 长轮询
  50. * @private
  51. */
  52. var LongPollingTransport = /** @class */ (function () {
  53. /**
  54. * 导出 request 工具
  55. * @param {Request} request
  56. * @param {((() => string | Promise<string>) | undefined)} accessTokenFactory access-token-factory
  57. * @param {ILogger} logger
  58. * @param {boolean} logMessageContent
  59. * @memberof LongPollingTransport
  60. */
  61. function LongPollingTransport(options) {
  62. this.accessTokenFactory = options.accessTokenFactory ? options.accessTokenFactory : undefined;
  63. this.logger = options.logger ? options.logger : new NullLogger();
  64. this.pollAbort = new AbortController();
  65. this.logMessageContent = options.logMessageContent ? options.logMessageContent : false;
  66. this.request = options.request ? options.request : new DefaultRequest({}, this.logger);
  67. this.running = false;
  68. this.onreceive = null;
  69. this.onclose = null;
  70. }
  71. Object.defineProperty(LongPollingTransport.prototype, "pollAborted", {
  72. // This is an internal type, not exported from 'index' so this is really just internal.
  73. get: function () {
  74. return this.pollAbort.aborted;
  75. },
  76. enumerable: true,
  77. configurable: true
  78. });
  79. /**
  80. * 连接 - 这里理解为请求
  81. *
  82. * @param {string} url
  83. * @param {TransferFormat} transferFormat
  84. * @returns {Promise<void>}
  85. * @memberof LongPollingTransport
  86. */
  87. LongPollingTransport.prototype.connect = function (options) {
  88. return __awaiter(this, void 0, void 0, function () {
  89. var pollOptions, token, response;
  90. return __generator(this, function (_a) {
  91. switch (_a.label) {
  92. case 0:
  93. /* 验证参数完整性,不完整抛出异常 */
  94. Arg.isRequired(options, "options");
  95. Arg.isRequired(options.transferFormat, "transferFormat");
  96. Arg.isIn(options.transferFormat, TransferFormat, "transferFormat");
  97. // update options
  98. this.url = options.url;
  99. // print log
  100. this.logger.log(LogLevel.Trace, "(LongPolling transport) Connecting.");
  101. pollOptions = {
  102. config: {
  103. // 中断信号
  104. about: this.pollAbort.signal.aborted,
  105. timeout: 120 * 1000 // 超时时间 2 min
  106. },
  107. // origin header 头
  108. headers: {}
  109. };
  110. if (options.transferFormat === TransferFormat.Binary) {
  111. pollOptions.responseType = ResponseType.ARRAY_BUFFER;
  112. }
  113. return [4 /*yield*/, this.getAccessToken()];
  114. case 1:
  115. token = _a.sent();
  116. this.updateHeaderToken(pollOptions, token);
  117. // Make initial long polling request
  118. // Server uses first long polling request to finish initializing connection and it returns without data
  119. //发出初始长轮询请求
  120. //服务器使用第一个长轮询请求完成连接初始化,它返回时不带数据
  121. this.logger.log(LogLevel.Trace, "(LongPolling transport) polling: [url]" + this.url);
  122. return [4 /*yield*/, this.request.get(this.url, {
  123. _: Date.now()
  124. }, pollOptions)];
  125. case 2:
  126. response = _a.sent();
  127. if (response.statusCode !== 200) {
  128. this.logger.log(LogLevel.Error, "(LongPolling transport) Unexpected response code: " + response.statusCode + ".");
  129. // Mark running as false so that the poll immediately ends and runs the close logic
  130. // ! 重写了 内置 的 创建 `HttpError` 方法
  131. this.closeError = new HttpError(response.errMsg || "", response.statusCode);
  132. this.running = false;
  133. }
  134. else {
  135. this.running = true;
  136. }
  137. this.receiving = this.poll(this.url, pollOptions);
  138. return [2 /*return*/, Promise.resolve({
  139. errMsg: "connect success"
  140. })];
  141. }
  142. });
  143. });
  144. };
  145. /**
  146. * 获取 access-token
  147. *
  148. * @private
  149. * @returns {(Promise<string | null>)}
  150. * @memberof LongPollingTransport
  151. */
  152. LongPollingTransport.prototype.getAccessToken = function () {
  153. return __awaiter(this, void 0, void 0, function () {
  154. return __generator(this, function (_a) {
  155. switch (_a.label) {
  156. case 0:
  157. if (!this.accessTokenFactory) return [3 /*break*/, 2];
  158. return [4 /*yield*/, this.accessTokenFactory()];
  159. case 1: return [2 /*return*/, _a.sent()];
  160. case 2: return [2 /*return*/, null];
  161. }
  162. });
  163. });
  164. };
  165. /**
  166. * 更新 access-token
  167. *
  168. * @private
  169. * @param {RequestOption} request
  170. * @param {(string | null)} token
  171. * @returns
  172. * @memberof LongPollingTransport
  173. */
  174. LongPollingTransport.prototype.updateHeaderToken = function (request, token) {
  175. /**
  176. * fix header
  177. */
  178. if (!request.headers) {
  179. request.headers = {};
  180. }
  181. /**
  182. * push token to headers
  183. */
  184. if (token) {
  185. // tslint:disable-next-line:no-string-literal
  186. request.headers["Authorization"] = "Bearer " + token;
  187. return;
  188. }
  189. // tslint:disable-next-line:no-string-literal
  190. if (request.headers["Authorization"]) {
  191. // tslint:disable-next-line:no-string-literal
  192. delete request.headers["Authorization"];
  193. }
  194. };
  195. /**
  196. * 异步计数?
  197. *
  198. * @private
  199. * @param {string} url
  200. * @param {RequestOption} pollOptions
  201. * @returns {Promise<void>}
  202. * @memberof LongPollingTransport
  203. */
  204. LongPollingTransport.prototype.poll = function (url, pollOptions) {
  205. return __awaiter(this, void 0, void 0, function () {
  206. var token, pollUrl, response, e_1;
  207. return __generator(this, function (_a) {
  208. switch (_a.label) {
  209. case 0:
  210. _a.trys.push([0, , 8, 9]);
  211. _a.label = 1;
  212. case 1:
  213. if (!this.running) return [3 /*break*/, 7];
  214. return [4 /*yield*/, this.getAccessToken()];
  215. case 2:
  216. token = _a.sent();
  217. this.updateHeaderToken(pollOptions, token);
  218. _a.label = 3;
  219. case 3:
  220. _a.trys.push([3, 5, , 6]);
  221. pollUrl = url + "&_=" + Date.now();
  222. this.logger.log(LogLevel.Trace, "(LongPolling transport) polling: " + pollUrl + ". - (fy:\u957F\u8F6E\u8BE2\u4F20\u8F93 - polling)");
  223. return [4 /*yield*/, this.request.get(this.url, {
  224. _: Date.now()
  225. }, pollOptions)];
  226. case 4:
  227. response = _a.sent();
  228. if (response.statusCode === 204) {
  229. this.logger.log(LogLevel.Information, "(LongPolling transport) Poll terminated by server. - (fy:长轮询传输 - 由服务器终止轮询。)");
  230. this.running = false;
  231. }
  232. else if (response.statusCode !== 200) {
  233. this.logger.log(LogLevel.Error, "(LongPolling transport) Unexpected response code: " + response.statusCode + ". - (fy:\u957F\u8F6E\u8BE2\u4F20\u8F93 - \u610F\u5916\u7684\u54CD\u5E94\u4EE3\u7801)");
  234. // Unexpected status code
  235. this.closeError = new HttpError(response.errMsg || "", response.statusCode);
  236. this.running = false;
  237. }
  238. else {
  239. // Process the response
  240. if (response.data) {
  241. this.logger.log(LogLevel.Trace, "(LongPolling transport) data received. " + getDataDetail(response.data, this.logMessageContent) + ".");
  242. if (this.onreceive) {
  243. this.onreceive(response.data);
  244. }
  245. }
  246. else {
  247. // This is another way timeout manifest.
  248. this.logger.log(LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing.");
  249. }
  250. }
  251. return [3 /*break*/, 6];
  252. case 5:
  253. e_1 = _a.sent();
  254. if (!this.running) {
  255. // Log but disregard errors that occur after stopping - fy: 记录但忽略停止后发生的错误
  256. this.logger.log(LogLevel.Trace, "(LongPolling transport) Poll errored after shutdown: " + e_1.message);
  257. }
  258. else {
  259. if (e_1 instanceof TimeoutError) {
  260. // Ignore timeouts and reissue the poll. - 忽略超时并重新发出投票
  261. this.logger.log(LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing. - (fy:长轮询传输 - 请求超时)");
  262. }
  263. else {
  264. // Close the connection with the error as the result.
  265. this.closeError = e_1;
  266. this.running = false;
  267. }
  268. }
  269. return [3 /*break*/, 6];
  270. case 6: return [3 /*break*/, 1];
  271. case 7: return [3 /*break*/, 9];
  272. case 8:
  273. this.logger.log(LogLevel.Trace, "(LongPolling transport) Polling complete. - (fy:长轮询传输 - 请求完成)");
  274. // We will reach here with pollAborted==false when the server returned a response causing the transport to stop.
  275. // If pollAborted==true then client initiated the stop and the stop method will raise the close event after DELETE is sent.
  276. //当服务器返回导致传输停止的响应时,我们将使用pollAborted==false到达这里。
  277. //如果pollAborted==true,则客户端启动了stop,stop方法将在发送DELETE后引发close事件。
  278. if (!this.pollAborted) {
  279. this.raiseOnClose();
  280. }
  281. return [7 /*endfinally*/];
  282. case 9: return [2 /*return*/];
  283. }
  284. });
  285. });
  286. };
  287. /**
  288. * 发送轮询包
  289. *
  290. * @param {*} data
  291. * @returns {Promise<void>}
  292. * @memberof LongPollingTransport
  293. */
  294. LongPollingTransport.prototype.send = function (data) {
  295. return __awaiter(this, void 0, void 0, function () {
  296. return __generator(this, function (_a) {
  297. if (!this.running) {
  298. return [2 /*return*/, Promise.reject(new Error("Cannot send until the transport is connected"))];
  299. }
  300. return [2 /*return*/, sendMessage(this.logger, "LongPolling", this.request, this.url, this.accessTokenFactory, data, this.logMessageContent)];
  301. });
  302. });
  303. };
  304. /**
  305. * 停止
  306. *
  307. * @returns {Promise<void>}
  308. * @memberof LongPollingTransport
  309. */
  310. LongPollingTransport.prototype.stop = function () {
  311. return __awaiter(this, void 0, void 0, function () {
  312. var deleteOptions, token, e_2;
  313. return __generator(this, function (_a) {
  314. switch (_a.label) {
  315. case 0:
  316. this.logger.log(LogLevel.Trace, "(LongPolling transport) Stopping polling.");
  317. // Tell receiving loop to stop, abort any current request, and then wait for it to finish
  318. this.running = false;
  319. this.pollAbort.abort();
  320. _a.label = 1;
  321. case 1:
  322. _a.trys.push([1, 5, 6, 7]);
  323. return [4 /*yield*/, this.receiving];
  324. case 2:
  325. _a.sent(); // 这里 receiving 本身是一个 promise result, 用这个对象来监控请求未完成
  326. // Send DELETE to clean up long polling on the server
  327. // 发送DELETE以清除服务器上的长轮询
  328. this.logger.log(LogLevel.Trace, "(LongPolling transport) sending DELETE request to " + this.url + ".");
  329. deleteOptions = {
  330. headers: {}
  331. };
  332. return [4 /*yield*/, this.getAccessToken()];
  333. case 3:
  334. token = _a.sent();
  335. this.updateHeaderToken(deleteOptions, token);
  336. return [4 /*yield*/, this.request.delete(this.url, {}, deleteOptions)];
  337. case 4:
  338. _a.sent();
  339. this.logger.log(LogLevel.Trace, "(LongPolling transport) DELETE request sent.");
  340. return [2 /*return*/, Promise.resolve({
  341. errMsg: "stop success"
  342. })];
  343. case 5:
  344. e_2 = _a.sent();
  345. this.logger.log(LogLevel.Error, "(LongPolling transport) Stop error.", e_2);
  346. return [2 /*return*/, Promise.reject({
  347. errMsg: "stop fail"
  348. })];
  349. case 6:
  350. this.logger.log(LogLevel.Trace, "(LongPolling transport) Stop finished.");
  351. // Raise close event here instead of in polling
  352. // It needs to happen after the DELETE request is sent
  353. //在此处引发关闭事件,而不是在轮询中
  354. //它需要在发送删除请求后发生
  355. this.raiseOnClose();
  356. return [7 /*endfinally*/];
  357. case 7: return [2 /*return*/];
  358. }
  359. });
  360. });
  361. };
  362. /**
  363. * 调用关闭回调
  364. *
  365. * @private
  366. * @memberof LongPollingTransport
  367. */
  368. LongPollingTransport.prototype.raiseOnClose = function () {
  369. if (this.onclose) {
  370. var logMessage = "(LongPolling transport) Firing onclose event.";
  371. if (this.closeError) {
  372. logMessage += " Error: " + this.closeError;
  373. }
  374. this.logger.log(LogLevel.Trace, logMessage);
  375. this.onclose(this.closeError);
  376. }
  377. };
  378. return LongPollingTransport;
  379. }());
  380. export { LongPollingTransport };